简体   繁体   中英

MySQLdb Issue with Python 2.7.6 on Yosemite

I'm needing to connect to a MySQL database that is on my local machine via a Python script. Here's my setup:

  • Installed (and have been using for developing web apps for several months) MySQL 5.6.22 (downloaded .dmg from here )
  • Running the pre-installed python (2.7) that came with Yosemite
  • Downloaded: MySQL-python-1.2.4b4.tar.gz and installed via terminal by running python setup.py install in the unzipped folder

This is the output I got at the end of the installation:

Installed /Library/Python/2.7/site-packages/distribute-0.6.28-py2.7.egg
Processing dependencies for distribute==0.6.28`
Finished processing dependencies for distribute==0.6.28
Processing MySQL_python-1.2.4b4-py2.7-macosx-10.10-intel.egg
creating /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.10-intel.egg
Extracting MySQL_python-1.2.4b4-py2.7-macosx-10.10-intel.egg to /Library/Python/2.7/site-packages
Adding MySQL-python 1.2.4b4 to easy-install.pth file

Installed /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.10-intel.egg
Processing dependencies for MySQL-python==1.2.4b4
Finished processing dependencies for MySQL-python==1.2.4b4

But when trying to connect, my py script gives me this error:

ImportError: dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.10-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.10-intel.egg/_mysql.so
Reason: image not found
('result', 256)

When I do

$ python
>>> import MySQLdb

I get the same error.

Any ideas on how I might get this sorted out?

First, you should make sure you have libmysqlclient.18.dylib . On my mac, it's path is: /opt/local/lib/mysql56/mysql/libmysqlclient.18.dylib . If you don't have, you may need to get it by installing mysql5-devel.

If you have the dynamic library, the reason is that _mysql.so's information is wrong. You can use otool command to check it.

otool -L /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.10-intel.egg/_mysql.so

It will show that the path this command tells you is not the correct path of the dynamic library. You can use the following command to fix it.

sudo install_name_tool -change libmysqlclient.18.dylib {this is the correct path for mysql dynamic library} /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.10-intel.egg/_mysql.so

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM