Sunday, November 11, 2012

Pyramid, MySQL, and Python 3

For the impatient

Assuming you have Python 3 installed with distribute and virtualenv and also MySQL, create a virtualenv

virtualenv --no-site-packages --distribute myenv

Then activate your environment

source myenv/bin/activate

Create the following requirements file (save to requirements.txt):

#oursql==0.9.3 but need special Python 3 build https://bugs.launchpad.net/oursql/+bug/1051512
https://launchpad.net/oursql/py3k/py3k-0.9.3/+download/oursql-0.9.3.zip
pyramid==1.3.4

Then run:

pip install -r requirements.txt

The longer version

I'm currently working on an Android app version of FuelMyRoute.com. The website currently uses TurboGears (version 1.0.something I'm afraid). I thought about using the website as the backend for the mobile app but wanted the freedom to experiment and potentially do things much differently in a separate mobile backend. I also wanted to work on a more modern stack. The TurboGears team has essentially joined forces with the Pyramid team, so I took a look at Pyramid. I liked what I saw, and with version 1.3 it can run on Python 3.

So let's see if we can get a Pyramid/SQLAlchemy/MySQL site working on Python 3. The first thing I ran into is that it was hard to find a good Python MySQL driver that works on Python 3. For years I've been using the trusty if somewhat antiquated MySQLdb Python module. It looks like development has slowed to a halt on MySQLdb so I took a look at alternatives.

First alternative I explored was MySQL Connector/Python. I like that Oracle is sponsoring the development and it is a pure Python library. However, I hit upon a major bug that made it a deal breaker. I've subsequently learned that this bug has been fixed so I'll probably give it another try in the future.

Second alternative I looked at was OurSQL. I'm not sure if it is a fork of MySQLdb, but it seems fairly similar. It does have some benefits over MySQLdb , not the least of which is Python 3 support. One catch is that you can't just "pip install oursql" under Python 3. There is a distinct branch of code for the Python 3 support. To install the Python 3 branch of OurSQL you can use the requirements.txt file above, or for just OurSQL you can do

pip install https://launchpad.net/oursql/py3k/py3k-0.9.3/+download/oursql-0.9.3.zip

I also ran into an issue with Python 3 and one of Pyramid's dependencies, zope.interface. That's why zope.interface is listed in the requirements.txt file with version 3.8.0. But this is fixed now https://github.com/Pylons/pyramid/issues/604. Just make sure you get the latest 1.3 bugfix release. At the time of this writing, that is 1.3.4.

To get started with SQLAlchemy, just run the following to create a Pyramid scaffold project using SQLAlchemy:

pcreate -s alchemy MyProject
More info here: http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/project.html

No comments: