Python mysqldb: Library not loaded: libmysqlclient.18.dylib
Asked Answered
W

15

178

I just compiled and installed mysqldb for python 2.7 on my mac os 10.6. I created a simple test file that imports

import MySQLdb as mysql

Firstly, this command is red underlined and the info tells me "Unresolved import". Then I tried to run the following simple python code

import MySQLdb as mysql

def main():
    conn = mysql.connect( charset="utf8", use_unicode=True, host="localhost",user="root", passwd="",db="" )

if __name__ == '__main__'():
    main()

When executing it I get the following error message

Traceback (most recent call last):
  File "/path/to/project/Python/src/cvdv/TestMySQLdb.py", line 4, in <module>
    import MySQLdb as mysql
  File "build/bdist.macosx-10.6-intel/egg/MySQLdb/__init__.py", line 19, in <module>
    \namespace cvdv
  File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/toom/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/toom/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so
  Reason: image not found

What might be the solution to my problem?

EDIT: Actually I found out that the library lies in /usr/local/mysql/lib. So I need to tell my pydev eclipse version where to find it. Where do I set this?

Whitt answered 17/6, 2011 at 8:37 Comment(0)
W
325

I solved the problem by creating a symbolic link to the library. I.e.

The actual library resides in

/usr/local/mysql/lib

And then I created a symbolic link in

/usr/lib

Using the command:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

so that I have the following mapping:

ls -l libmysqlclient.18.dylib 
lrwxr-xr-x  1 root  wheel  44 16 Jul 14:01 libmysqlclient.18.dylib -> /usr/local/mysql/lib/libmysqlclient.18.dylib

That was it. After that everything worked fine.

EDIT:

Notice, that since MacOS El Capitan the System Integrity Protection (SIP, also known as "rootless") will prevent you from creating links in /usr/lib/. You could disable SIP by following these instructions, but you can create a link in /usr/local/lib/ instead:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
Whitt answered 6/8, 2011 at 15:28 Comment(9)
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylibMorrison
I had installed mysql55 via MacPorts and to solve this error did: sudo ln -s mysql/libmysqlclient.18.dylib /opt/local/lib/mysql55/libmysqlclient.18.dylibFivefold
After Mavericks removed my old symlink, I had to symlink from a slightly different spot: sudo ln -s /usr/local/mysql-5.5.29-osx10.6-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylibEarnestineearnings
You will have to disable SIP if you're running on El Capitan: forums.developer.apple.com/thread/7935.Cartilage
After a fresh install of El Capitan, I have found that you do not need to disable SIP for this tip.Shortfall
Yep, I also just followed the steps mentioned here, without disabling SIP, and it works fine. Of course, I still get a undefined variable from import: connect error statement, but he code actually works fine. So all one might need to do is to disable the error, as described here: https://mcmap.net/q/56662/-pydev-undefined-variable-from-import-errorDowning
I upgraded to El Capitan from Mavericks, and was unable to create the symlink because of SIP. However, Caleb Shay's answer using install_name_tool to change the path for libmysqlclient.18.dylib works well, and doesn't even require sudo if in a virtualenv.Stanleigh
agree with @caleb shay, always look for solutions which avoid changing system properties. (answer below)Maemaeander
It's not really necessary to disable SIP. Simply use the "local" directory: sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylibMidwifery
M
139

My preferred method is to actually fix the library rather than playing with environment variables that may or may not actually be in scope depending on how the application is run. This is actually a fairly simple process.

First, look at the error output to see where the offending python module is located:

ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Library/Python/2.7/site-packages/_mysql.so Reason: image not found

Okay, so the offending file is /Library/Python/2.7/site-packages/_mysql.so

Next, figure out where _mysql.so thinks it should find libmysqlclient.18.dylib:

% otool -L /Library/Python/2.7/site-packages/_mysql.so
/Library/Python/2.7/site-packages/_mysql.so:
    libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

So, it's looking for libmysqlclient.18.dylib with no path information, let's fix that:

% sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Library/Python/2.7/site-packages/_mysql.so

Now _mysql.so knows the full path to the library and everything works, regardless of environment variables.

% otool -L /Library/Python/2.7/site-packages/_mysql.so                                                                                           
/Library/Python/2.7/site-packages/_mysql.so:
    /usr/local/mysql/lib/libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)
Myungmyxedema answered 16/11, 2012 at 17:56 Comment(5)
Wouldn't this be an even better solution so it fixes it with all virtualenvs? sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib $VIRTUAL_ENV/lib/python2.7/site-packages/_mysql.soCoben
@BradRuderman I guess it's important to be clear that when you run the command you have given, you fix one virtualenv only - your current one. Furthermore, not everyone is (to their detriment) running in a virtualenv, so that command line would not be as general a solution as the one posted.Claustral
Important note the example given is fixing the global python/mysql you will need to fix it in each one of your Virtual Environments. If you're like me you glazed right over that first part where they are location _mysql.so thats an important step.Magnetoelectricity
This works for _mysql.so installed inside VirtualEnvironments, and doesn't require disabling SIP in El Capitan.Stanleigh
installing MySQL from dmg, mysql-connector-c from brew, and then making this change is what finally got MySQLdb working on High Sierra after 2 days of pain.Perdue
T
60

I found there was another solution for this problem rather than creating a symbolic link.

You set the path to your directory, where libmysqlclient.18.dylib resides, to DYLD_LIBRARY_PATH environment variable. What I did is to put following line in my .bash_profile:

export DYLD_LIBRARY_PATH=/usr/local/mysql-5.5.15-osx10.6-x86/lib/:$DYLD_LIBRARY_PATH

That's it.

Telegraphic answered 10/8, 2011 at 3:18 Comment(1)
there is also a symlink at /usr/local/mysql which points to the installed version, so I suggest changing your line to: export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:$DYLD_LIBRARY_PATHPanathenaea
H
37

In my case, I was getting the error with Mac OS X 10.9 Mavericks. I installed MySQL Community Server directly from the Oracle/MySQL Website from DMG.

All I needed to do was symlink the lib files to the /usr/local/lib directory.

mkdir -p /usr/local/lib   
ln -s /usr/local/mysql/lib/libmysql* /usr/local/lib

Bonus: If you're running Mac OS X as well, there is a great tool to finding files like the libmysqlclient.18.dylib file, http://apps.tempel.org/FindAnyFile. This is how I originally found the location of the dylib file.

Heywood answered 27/10, 2013 at 20:30 Comment(1)
I had to create the /usr/local/lib directory, but worked like a charm!Dratted
H
26

I found putting this in your .profile or .bashrc (whichever you use) is the easiest way to do it, sym links are messy compared to keeping paths in your source files.

Also compared to yoshisurfs answer, most of the time when mysql gets installed the mysql directory should be renamed to just mysql, not the whole file name, for ease of use.

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
Haggi answered 29/6, 2012 at 16:9 Comment(1)
This seems like a really sane and simple answer. Worked well for me - thanks!Candleberry
L
5

I've run into this with a couple virtual environments.

pip uninstall MySQL-python
pip install -U MySQL-python

Worked both times.

Lycaon answered 29/6, 2017 at 18:14 Comment(1)
Worked for me tooKoetke
O
3

In pydev eclipse plugin, you may want to set the environment variable for DYLD. The path can be set as shown in

Install mysqldb on snow leopard

Outstretched answered 30/6, 2011 at 15:40 Comment(0)
A
3

For those using homebrew you might fix this with:

$ brew link mysql
Ashleyashli answered 1/2, 2015 at 17:8 Comment(1)
This worked for me, it created a link like this: /usr/local/lib/libmysqlclient.18.dylib -> /usr/local/Cellar/mysql/5.6.27/lib/libmysqlclient.18.dylibWhelm
G
2

when you are in El Capitan, will get error: ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted need to close the "System Integrity Protection".

first, reboot and hold on cmd + R to enter the Recovery mode, then launch the terminal and type the command: csrutil disable, now you can reboot and try again.

Gout answered 9/9, 2015 at 6:15 Comment(1)
Another way you can move file libmysqlclient.18.dylib to /usr/local/lib and add path PATH=/usr/local/lib:$PATH to bash_profile. It's working for me.Ptolemaeus
M
2

In my Case, in El Capitan (OSX 10.11), I have to do following in ~/.bash_profile

export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:${DYLD_LIBRARY_PATH}"
export PATH="/usr/local/mysql/lib:${PATH}"
Mclemore answered 4/11, 2015 at 10:47 Comment(0)
M
1

On new El Capitan installation where SIP(rootless prevents access to usr/lib/) is on by default and you cannot create the symlink unless you are in recovery mode. As @yannisxu said you can disable SIP and do your symlink to /usr/lib/local and this will work.

you can use the following command on MAC OSX El Capitan instead of turning off SIP:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

There used to be an option where you can login as root and this can disable SIP but in the final release that is now obsolete, you can read more about it here: https://forums.developer.apple.com/thread/4686

Question:

There is a nvram boot-args command available in Developer Beta 1 which can disable SIP when run with root privileges:

nvram boot-args="rootless=0"

Will this option of disabling SIP also be available in the El Capitan release version? Or is this strictly for the Developer Builds?

Answer:

This nvram boot-args command will be going away. It will not be available in the El Capitan release version and may disappear before the end of the Developer Betas. Keep an eye on the release notes for future Developer Betas.

Multitude answered 7/10, 2015 at 9:16 Comment(0)
D
0

I had this issue and it took me for a while to figure out how to fix that.

My case is slightly different. My MySQL server is of version 5.1.x. And somehow I upgraded my MySQL-python from 1.2.3 to 1.2.5. And I kept getting this issue since then event I added the following soft link.

libmysqlclient.18.dylib -> /usr/local/mysql/lib/libmysqlclient.18.dylib

It turns out that for MySQL 5.1.x there is no libmysqlclient.18.dylib, but only libmysqlclient.16.dylib. You can fix this issue either by downgrade your MySQL-python to 1.2.3 or upgrade your MySQL server to 5.6.x (I haven't tried 5.5.x.)

I downgraded the library to 1.2.3 since upgrading MySQL is not an option for me.

Debus answered 14/7, 2014 at 15:19 Comment(0)
B
0

go to http://dev.mysql.com/downloads/connector/c/ and download MySQL Connector/C. after getting the package, make a new directory 'mysql', uncompress the Mysql Connector file under directory mysql, then under mysql, make another empty directory 'build'.we will use 'build' to build MySQL Connector/C. cd build && cmake ../your-MySQL-Connector-source-dir make && make install after make install, you will get a directory named mysql under /usr/local. it contains all the headers and libs you need.go to this dirctory, and copy the headers and libs to corresponding locations.

Buyers answered 27/7, 2016 at 8:2 Comment(0)
A
0

you can try:

sudo install_name_tool -change libmysqlclient.18.dylib /Users/toom/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so`
Amorette answered 10/2, 2018 at 1:58 Comment(0)
M
0

Note about bug of MySQL Connector/C on macOS (my current version is 10.13.2), fix the mysql_config and reinstall mysqlclient or MySQL-python, here is the detail

Merge answered 28/4, 2019 at 2:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.