Mac Python 无法使用 MySQLdb

2017-07-03 11:45:51 +08:00
 kwdfmzhu

操作系统:

Mac os,版本:10.12.5,64bit Mysql:版本 5.7.18 Homebrew

使用 brew 安装了 mysql ( Server version: 5.7.18 Homebrew ) ,然后下载了 MySQLdb,正确安装尅没有报错,安装成功后报错,报错如下:

MySQL_python-1.2.5-py2.7.egg-info lazybone$ python -c "import MySQLdb"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.12-intel.egg/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.12-intel.egg/_mysql.so, 2): Symbol not found: _mysql_affected_rows
  Referenced from: /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.12-intel.egg/_mysql.so
  Expected in: flat namespace
 in /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.12-intel.egg/_mysql.so

尝试如下解决方案,均未果:

  1. 替换 MySQL_python-1.2.5,也无效;
  2. 重新安装 pip uninstall MySQL-python,brew uninstall mysql,brew install mysql --universal,pip install MySQL-python
  3. 增加环境变量配置,export DYLD_LIBRARY_PATH=/usr/local/Cellar/mysql/5.7.18_1/lib/,export DYLD_LIBRARY_PATH=.:$DYLD_LIBRARY_PATH

_mysql.so 相关信息:

MySQL_python-1.2.5-py2.7.egg-info lazybone$ file /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.12-intel.egg/_mysql.so
/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.12-intel.egg/_mysql.so: Mach-O universal binary with 2 architectures: [x86_64: Mach-O 64-bit bundle x86_64] [i386: Mach-O bundle i386]
/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.12-intel.egg/_mysql.so (for architecture x86_64): Mach-O 64-bit bundle x86_64
/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.12-intel.egg/_mysql.so (for architecture i386): Mach-O bundle i386

ps:同样机器配置,同事可以安装并使用!!!( Mysql 5.7.18 Homebrew + MySQL_python-1.2.4b4 )

6133 次点击
所在节点    Python
26 条回复
kwdfmzhu
2017-07-03 19:08:28 +08:00
@scriptB0y 版本都是对的,我下午的时候换了一台电脑,同样的步骤安装后就没有问题,看来应该是环境的问题引起的,具体是哪个环境估计得好好找了。。。
zzd1000
2017-07-03 19:36:21 +08:00
sudo install_name_tool -change libmysqlclient.18.dylib \
/usr/local/mysql/lib/libmysqlclient.18.dylib \
/Library/Python/2.7/site-packages/_mysql.so
mingyun
2017-07-03 22:50:24 +08:00
pymysql +1
reyno
2017-07-03 23:26:31 +08:00
pip install pymysql
SlipStupig
2017-07-03 23:32:59 +08:00
需要 mysql 开发包才能编译过....不知道支不支持 mac,linux 下面一切正常
chengxiao
2017-07-04 07:39:51 +08:00
我昨天也是因为这个原因换了 pymysql ……

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/372630

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX