Tuesday, July 13, 2010

Error Trying To Import MySQLdb

Well, I didn't take long to hit an error. I was going through the QuickWiki [as of today 7.13.10 this tutorial is so our of date its not worth attempting] tutorial on the Pylons site to try to get some basic database functionality working as a proof-of-concept for myself when I see the following error:

load_environment(conf.global_conf, conf.local_conf)
File "/Users/josephmisiti/Downloads/mydevenv/MyBlog/myblog/config/environment.py", line 48, in load_environment
engine = engine_from_config(config, 'sqlalchemy.')
File "/Users/josephmisiti/Downloads/mydevenv/lib/python2.5/site-packages/SQLAlchemy-0.5.8-py2.5.egg/sqlalchemy/engine/__init__.py", line 241, in engine_from_config
return create_engine(url, **opts)
File "/Users/josephmisiti/Downloads/mydevenv/lib/python2.5/site-packages/SQLAlchemy-0.5.8-py2.5.egg/sqlalchemy/engine/__init__.py", line 223, in create_engine
return strategy.create(*args, **kwargs)
File "/Users/josephmisiti/Downloads/mydevenv/lib/python2.5/site-packages/SQLAlchemy-0.5.8-py2.5.egg/sqlalchemy/engine/strategies.py", line 62, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/Users/josephmisiti/Downloads/mydevenv/lib/python2.5/site-packages/SQLAlchemy-0.5.8-py2.5.egg/sqlalchemy/databases/mysql.py", line 1456, in dbapi
import MySQLdb as mysql
File "build/bdist.macosx-10.5-i386/egg/MySQLdb/__init__.py", line 19, in
File "build/bdist.macosx-10.5-i386/egg/_mysql.py", line 7, in
File "build/bdist.macosx-10.5-i386/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dynamic module does not define init function (init_mysql)

To make sure this not an isolated problem in your Pylons framework, open up the terminal and type the following

python
>>>import MySQLdb

If you see the following error, continue reading, else I recommend installing the python module in your Pylon's virtual environment via 'easy_install.py MySQLdb'.

I am currently running OSX Leaport 10.5.8, and it appears that I had somehow accidently installed MySQL 5 for version 10.6 with 64-bit libraries. This is a problem because the MySQLdb is looking for 32-bit libraries and it seems to be causing this import error. For more information on the problem, please read the following comments in this link:

http://www.davidcramer.net/code/57/mysqldb-on-leopard.html

To fix the problem, I first uninstalled the MySQL on my MAC using the following procedure:

http://tomkeur.net/39/how-to-remove-mysql-completely-mac-os-x-leopard.html

For some reason, the mysql on my machine was no located in /usr/local/bin so I did a

find / -name "mysql5" -print 2> /dev/null

and deleted every single mysql search path that was returned from this query.

After I successfully uninstalled MySQL, I went to the mysql site and downloaded the correct package: MySQL 5.* 10.5.8 x86 and reinstalled the package via these instructions:

http://developer.apple.com/internet/opensource/osdb.html

After confirming that the installation was successful, I downloaded a new version of MySQLdb, unpacked it, and did a cd into the directory and executed the following commands

sudo python setup.py clean - remove all the current versions
sudo python setup.py install

Everything build and installed correctly, and I confirmed everything was working via the:

python
>>>import MySQLdb

command.

Good luck with this issue, and I hope this helps.

No comments:

Post a Comment