Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error importing MySQL package for Python

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?

like image 605
Pav Sidhu Avatar asked Jul 23 '15 19:07

Pav Sidhu


1 Answers

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.

like image 110
letitbee Avatar answered Oct 02 '22 21:10

letitbee