Forums

No module named mysql.connector

Hi there ! I am trying to run a server written in Python using the mysql.connector module. Unfortunately it doesn't seem to be installed, as I get an exception and see this error in my error log:

2013-01-05 14:03:22,184 :ImportError: No module named mysql.connector

Any chance to get that installed, or are there other means to connect to my MySQL database with Python ?

The module can be obtained from http://dev.mysql.com/usingmysql/python/

Regards ... Axel

We'll look at getting mysql.connector installed. In the meantime, we have mysql-python and oursql installed, so you could use those.

The "standard" Python module for MySQL access is MySQLdb, which is available on PAW - it more or less follows PEP-249, which is a standard interface to the vast majority of Python database modules. If you have specific problems with it, feel free to ask, I'm sure many of the users on the forums have experience with it.

OK, thanks for mentioning these other modules. MySQLdb works nicely. Fortunately using it does not require much re-write in my code.

Great, glad it wasn't too hard for you to update.

It's generally a good idea to always try to use database modules that conform to PEP-249 where you can - that way, if you change your database engine then your code changes should be fairly minimal (just SQL dialect changes in the ideal case). Of course, if you've created a minimal API layer around the database then the rest of your application should be insulated anyway.

I'd like to use mysql.connector rather than MySQLdb for now, as the former supports 3.3 - can I just install it myself?

You should just be able to do:

pip-3.3 install --user mysql-connector-python

Fantastic, that worked - thanks Glenn!

Update: the recommended MySQL library for Python 3.x has now changed. If you're not using a virtualenv then you don't need to install anything -- it's already installed and you just need to import MySQLdb. If you are using a virtualenv, then start a bash console inside the virtualenv and run

pip install mysqlclient