Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, Brew, and MySQLdb

I have been running python from a brew install. I went to install the mysql_python egg with setup tools (standard install according to mysql_python instructions) and it installed to /usr/local/lib/python2.7/site-packages/. The dependencies processed, etc.

Then I went to run python console. I can import other things (e.g. import django; print django.VERSION works) but when I import MySQLdb, I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.4-x86_64.egg/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: dlopen(/usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.4-x86_64.egg/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.4-x86_64.egg/_mysql.so
  Reason: image not found

Any insight? Thanks much.

like image 490
user592419 Avatar asked Sep 10 '11 22:09

user592419


People also ask

What is Python MySQLdb?

What is MYSQLdb? MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2. 0 and is built on top of the MySQL C API.

What is difference between PyMySQL and MySQL Connector?

PyMySQL is a pure-Python MySQL client library, based on PEP 249. Most public APIs are compatible with mysqlclient and MySQLdb. PyMySQL works with MySQL 5.5+ and MariaDB 5.5+. MySQL is a leading open source database management system.

Is Python compatible with MySQL?

MySQL server and Python versions within parentheses are known to work with Connector/Python, but are not officially supported. Bugs might not get fixed for those versions. Connector/Python does not support the old MySQL Server authentication methods, which means that MySQL versions prior to 4.1 will not work.


1 Answers

In trying to duplicate your error I did the following (I assume, being a homebrewer, you have done the same).

1) brew install python To install Python 2.7
2) brew install mysql To install mysql to the system (needed for various drivers)
3) Configured mysql per homebrew's recommendations
4) Downloaded mysql_python and unzipped it
5) Installed mysql_python using python setup.py install
6) Tested it in an interactive session python, import _mysql

After walking through these steps on Lion, I was unable to reproduce your error. Now... on to debugging your issue.

A few things to check:
1) In your terminal when you type which python does it point to your homebrew install?
2) Keep in mind that with homebrew the site-packages are not stored in the Cellar they are stored in /usr/local/lib/python2.7/site-packages. See this post for more info on why. I have not had to add the site-packages' location to my PATH, but you might give that a try.
3) The last thing I could suggest to try is in order to get easy_install to work with homebrew's python I had to add /usr/local/share/python to my PATH.

EDIT
After re-reading the error messages mainly after scrolling all the way to the right, I noticed that it was unable to load a mysql library. A quick google search on that library made it seem that when installing mysql on OS X there can be difficulties linking to it. Try and locate the file libmysqlclient.18.dylib and note down it's path. After installing mysql via homebrew mine is : /usr/local/Cellar/mysql/5.5.14/lib. The common fix I have seen fix the environment variable DYLD_LIBRARY_PATH. For more information where I got this check out this site. Using my path's as an example I would add this line to my .bash_profile

export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:/usr/local/Cellar/mysql/5.5.14/lib"

If this does not work for you, I would strongly look into the possibility that mysql isn't installed correctly or is having issues. With that mindset hopefully something will pop out at your.

Lastly, if you do not have any sql databaes yet, might I suggest uninstalling mysql and installing it via homebrew? I am now having zero trouble with that setup.

like image 121
Adam Lewis Avatar answered Oct 06 '22 15:10

Adam Lewis