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

咋破。快被折腾坏了

python mysql django

叫我兔子就好了 11 years ago

好吧,花了将近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了!

没睡醒的肉啊 answered 11 years ago

Your Answer