I installed MySQL and Python with Homebrew on OS X 10.10.5 Yosemite. My Python 2.7 is at python -> ../Cellar/python/2.7.9/bin/python
with a symlink to it at /usr/local/bin/python
.
In /usr/local/bin
there is a symlink:mysql -> ../Cellar/mysql/5.7.9/bin/mysql
In the Python shell:
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib
Referenced from: /usr/local/lib/python2.7/site-packages/_mysql.so
Reason: image not found
So I tried:$ sudo unlink /usr/local/lib/libmysqlclient.18.dylib
followed by:DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:$DYLD_LIBRARY_PATH
and then (desperation over reason):$ export DYLD_LIBRARY_PATH=/usr/local/Cellar/mysql/5.7.9/lib
But in both cases import MySQLdb
still tried to import libmysqlclient.18.dylib
.
Then I tried:$ pip install -U MySQL-python
and got:
Requirement already up-to-date: MySQL-python in /usr/local/lib/python2.7/site-packages
Many answers to this problem on SO suggest manually making an explicit symlink to the library with a version number (in my case libmysqlclient.20.dylib
). However, this seems crude and not future-proof, given the existing symlinks:
in /usr/local/lib
there islibmysqlclient.dylib -> ../Cellar/mysql/5.7.9/lib/libmysqlclient.dylib
and in /usr/local/Cellar/mysql/5.7.9/lib
we find:libmysqlclient.20.dylib
with a symlink in the same directory to it:
libmysqlclient.dylib -> libmysqlclient.20.dylib
libmysqlclient.18.dylib
?So how can I get Python to forget /usr/local/lib/libmysqlclient.18.dylib
and follow the correct symlink in in /usr/local/lib
to libmysqlclient.dylib
, without manually adding yet another symlink?
Solution: Fix ImportError: No module named MySQLdb It means, Python needs a module to interface with MySQL database and that modules does not seems to be present in the system. All you need to do is, just install MySQL-Python module and the script should work without any problem.
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.
I also encountered this problem. I uninstalled the MySQL-python, and then installed it.
pip uninstall MySQL-python
pip install MySQL-python
Update (based on comments)
In some cases, you may need to perform the second (install) step in the following manner:
pip install --no-binary MySQL-python MySQL-python
The no-binary
option is so that pip builds it fresh and links to the correct library:
--no-binary <format_control>
Do not use binary packages. Can be supplied multiple times, and each time adds to the existing value. Accepts either :all: to disable all binary packages, :none: to empty the set, or one or more package names with commas between them. Note that some packages are tricky to compile and may fail to install when this option is used on them.
NB: Note, that MySQL-python
needs to be mentioned twice. As mentioned above, the first occurrence is the name of the package to apply the no-binary
option to, the second specifies the package to install.
You need to use dev version of mysqlclient
:
pip install git+https://github.com/PyMySQL/mysqlclient-python.git@master
Before I had the lastest PyPI version (1.3.7) on Python 3.4 and it was searching for libmysqlclient.18.dylib
(from MySQL 5.6) whereas I had only libmysqlclient.20.dylib
(from MySQL 5.7).
If you use Python 3, MySQL-python
is not an option (and mysqlclient
is its newer version).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With