Using Connector/Python with SQLAlchemy

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:

shell> pip install SQLAlchemy
shell> 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.