玩蛇网提供最新Python编程技术信息以及Python资源下载!

Mac Python MySQLdb库遇到的一些问题

第一次执行

python setup.py build

报错:

sh: mysql_config: command not found
Traceback (most recent call last):
  File "setup.py", line 18, in <module>
    metadata, options = get_config()
  File "/Users/daodao/Desktop/Mysql/setup_posix.py", line 43, in get_config
    libs = mysql_config("libs_r")
  File "/Users/daodao/Desktop/Mysql/setup_posix.py", line 25, in mysql_config
    raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found

然后根据网上的解决办法,修改了setup_posix.py中

mysql_config.path = "mysql_config"

修改为我本机的mysql_config配置路径:

mysql_config.path = "/usr/local/mysql-5.6.14-osx10.7-x86_64/bin/mysql_config"

继续执行

python setup.py build

然后还是继续报错,但这次跟Xcode相关:

xcrun: error: active developer path ("/Applications/Xcode.app/Contents/Developer") does not exist, use xcode-select to change
error: command '/usr/bin/clang' failed with exit status 1

网上找了半天没有找到解决的办法。我早前曾安装后删除过Xcode

————————————

网上有人说好像是因为环境需要安装gcc,Xcode里面的command_line_tools 支持GGC,不管,先重新按照了下Xcode和command_line_tools,然后继续重复执行上面的操作。

python setup.py clean
python setup.py build
sudo python setup.py install

果然没有再报错了。但是问题还在继续,终端进入Python测试效果

import MySQLdb

果然还是不顺利。。。报错:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.6-intel.egg/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.6-intel.egg/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.6-intel.egg/_mysql.so
  Reason: image not found

咋破。快被折腾坏了

多谢。 一个环境变量给解决了。

好人啊!!我找这个问题找了好久....

OSX 10.11 可以用

sudo ln -s /usr/local/mysql-5.6.26-osx10.8-x86_64/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

http://www.53it.net/show/633.html

为什么,我在中断写 import MySQLdb 没有问题,但是在eclipse中缺报错

要是遇到ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted

在终端运行:sudo install_name_tool -change libmysqlclient.18.dylib \
/usr/local/mysql/lib/libmysqlclient.18.dylib \
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so

好吧,花了将近4个小时终于搞定这个问题了,最后一步骤的错误问题自问自答:

用vi指令在 User/打开 .bash_profile 文件,这是一个隐藏文件。

vi .bash_profile

进入编辑状态,在最后添加

export DYLD_LIBRARY_PATH="/usr/local/mysql/lib"

:w 保存退出bash

重新,切换到Python开发环境下, import MySQLdb 返回正常。

继续在Bash中输入 mysql -uroot

反馈:-bash: mysql: command not found。好吧,由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件。 首先得知道mysql命令或mysqladmin命令的完整路径,比如mysql的路径是:/usr/local/mysql/bin/mysql,然后执行命令:

ln -s /usr/local/mysql/bin/mysql /usr/bin

O了!

在10.11系统下还是提示:

>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.11-intel.egg/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.11-intel.egg/_mysql.so
  Reason: unsafe use of relative rpath libmysqlclient.18.dylib in /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.11-intel.egg/_mysql.so with restricted binary

如果执行:

ln -s /usr/local/mysql-5.6.26-osx10.8-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

会提示:

ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted

http://stackoverflow.com/questions/6383310/python-mysqldb-library-not-loaded-libmysqlclient-18-dylib#answer-13421926
参照这个吧

其实这个问题很简单,一步就可以搞定: ln -s /usr/local/mysql/bin/* /usr/local/bin
既可以避免mac的rootless 又可以解决mysql_xx 命令缺失的问题

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd20040.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年06月01日 - 16时59分39秒 发布自玩蛇网

您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容
我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules