Geert JM Vanderkelen

Using Connector/Python with SQLAlchemy

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 = '',
  db = 'test'),
  connect_args = {'time_zone': '+00:00'}

That’s it. Now just continue with SQLAlchemy as usual.

2 thoughts on “Using Connector/Python with SQLAlchemy

  1. Michael

    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:

    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__.pyc mxodbc.pyc mysqldb.pyc pyodbc.pyc zxJDBC.pyc

Leave a Reply

Your email address will not be published. Required fields are marked *

− 1 = 8

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>