7

I'm trying to import a MySQL module with python, more specifically Flask, though I receive an error. I'm using a virtual environment with my application. Here is the error:

    Traceback (most recent call last):
  File "../myapp/application.py", line 9, in <module>
    from flask.ext.mysql import MySQL
  File "/Users/pavsidhu/Documents/Web-Development/app/env/lib/python2.7/site-packages/flask/exthook.py", line 81, in load_module
    reraise(exc_type, exc_value, tb.tb_next)
  File "/Users/pavsidhu/Documents/Web-Development/app/env/lib/python2.7/site-packages/flaskext/mysql.py", line 3, in <module>
    import MySQLdb
  File "/Users/pavsidhu/Documents/Web-Development/app/env/lib/python2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: dlopen(/Users/pavsidhu/Documents/Web-Development/app/env/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: /Library/Python/2.7/site-packages/_mysql.so
  Referenced from: /Users/pavsidhu/Documents/Web-Development/app/env/lib/python2.7/site-packages/_mysql.so
  Reason: image not found

I can see in the error it says Library not loaded: /Library/Python/2.7/site-packages/_mysql.so. As I'm using a virtual environment that path is incorrect. It should be /lib/python2.7/site-packages/_mysql.so.

Is there a way to change this? Thanks.

EDIT:

I found there was a terminal command on OSX to change the library location:

 sudo install_name_tool -change libmysqlclient.18.dylib /lib/python2.7/site-packages/MySQLdb/

though after hitting enter I get this:

Usage: /Library/Developer/CommandLineTools/usr/bin/install_name_tool [-change old new] ... [-rpath old new] ... [-add_rpath new] ... [-delete_rpath old] ... [-id name] input

I don't appear to be entering the command wrong, what is the issue?

Pav Sidhu
  • 6,724
  • 18
  • 55
  • 110
  • 1
    how did you created the virtualenv? Does it use the system's site_packages (`--system-site-packages` in a new virtualenv version, enabled by default in older version ), or is it completely isolated? – yedpodtrzitko Jul 26 '15 at 00:45
  • Yes, It is completely isolated from the system. – Pav Sidhu Jul 26 '15 at 11:47
  • You're missing the last argument to `install_name_tool` command. Your command should read like, `install_name_tool -change old new input`. – Velimir Mlaker Jul 26 '15 at 18:33
  • So how would I go about changing the library that `_mysql.so`? refers to? – Pav Sidhu Jul 26 '15 at 23:59
  • 2
    Try setting LD_LIBRARY_PATH to include /lib/python2.7/site-packages/. This is a temporary, environment-specific way to achieve the permanent effect of install_name_tool. In bash: `export LD_LIBRARY_PATH=/lib/python2.7/site-packages/:${LD_LIBRARY_PATH}` – drootang Jul 27 '15 at 04:27
  • did you install the tool AFTER you activated your virtual environment? when you activate a virtual environment you will need to install all additional 3rd party packages again unless you are using and activating a shared virtualenv. if you have not installed the tool after activating the do that and run your app. – GHETTO.CHiLD Jul 27 '15 at 20:11

1 Answers1

2

As a rule, try avoiding system python install like a plague. Yes, even basing your virtualenvs on it. It will too often generate hard to understand problems.

I recommend getting Homebrew, then installing python and mysql for the headers:

brew install python
brew install mysql

And then basing your virtualenv on python from brew:

virtualenv venv --python /usr/local/bin/python

I know, it's a little more hoops to jump through, but it will make your development process that much easier. Not to mention brew is a great aid to a developer in its own right.

I just checked if it worked, it took me two minutes to get a working MySQLdb.

thule
  • 4,034
  • 21
  • 31