SQLAchemy has support for MySQL Connector/Python for a while now. Here is a little HOWTO showing how install both, and setup a database engine.
There are multiple ways of installing both projects, but here is the simplest using pip, whatever platform you use:
$ pip install SQLAlchemy
$ pip install mysql-connector-python
Start your SQLAlchemy engines using a URL pointing to Connector/Python. Note the connect_args
argument which passes extra connection arguments to Connector/Python. In the following example we set the MySQL session variable time_zone
to UTC:
from sqlalchemy import create_engine
DB_URI = "mysql+mysqlconnector://{user}:{password}@{host}:{port}/{db}"
engine = create_engine(DB_URI.format(
user ='sakila',
password = 'yoursecret',
host = '127.0.0.1',
db = 'test'),
connect_args = {'time_zone': '+00:00'}
)
That’s it. Now just continue with SQLAlchemy as usual.
Comments
I am getting the error on Mac OS: sqlalchemy.exc.NoSuchModuleError: Can’t load plugin: sqlalchemy.dialects:connector
The following folder contains mysql-connector-python driver: /usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mysql/connector/
The sqllalchemy/connectors/ folder has the following files: ls /usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/connectors
init.py init.pyc mxodbc.py mxodbc.pyc mysqldb.py mysqldb.pyc pyodbc.py pyodbc.pyc zxJDBC.py zxJDBC.pyc
@Michael:
You have to use “mysql+mysqlconnector” in the database URI, not “connector”.