Thursday, July 23, 2009

Running Pylons with Jython for Beginners

Well I'm sure someone out there besides me is interested in Pylons running on the JVM. Jython is a project working to bring Python to the JVM. If you are a Java user, and also a Pylons user you may be interested in the following.

To get started you need to install Jython. You can visit the Jython Site or get the 2.5.0 release here. You will need a version >= 2.5.0. The download is a jar based installer. You will need to run this as an application. On some platforms you can run the installer by double clicking the jar file (worked on OS X). On others you may need to go to the command line and issue this command "java -jar <jarname>".

Once Jython is installed setup easy_install for jython. Get (find it here). Once you have downloaded run it using "jython". This will install easy_install in Jython's bin folder.

Now if you work with Python you'll probably want to create an alias for this easy_install. I did. I just put the following in my .profile (note I also am aliasing virutualenv):
alias jeasy_install="$JYTHON_HME/bin/easy_install"
alias jeasy_install2.5="$JYTHON_HME/bin/easy_install-2.5"
alias jvirtualenv="$JYTHON_HME/bin/virtualenv"
Another note, don't use JYTHON_HOME in your bash env if you want virtualenv to work. I substituted JYTHON_HME. If you wan't to see why you can't use JYTHON_HOME check out the jython script. This needs to use JYTHON_HOME to configure the java classpath and run the Jython application.

Get virtualenv installed for Jython. Just type "jeasy_install virtualenv". Once that finishes you should have a 'virtualenv' tool in the Jython installation's bin folder. Note I have an example above of aliasing this to jvirtualenv, so that I can use the Python based version too.

Ok now you are ready for Pylons. You'll download the go-pylons script from PylonsHQ get this script here. Once it's downloaded run it using "jython jylons". This will create all the virtual environment where we can install packages specific to our app that we do not want polluting the Jython site-packages.

Alright getting closer... Activate your new env. On *nix this is as easy as "source jylons/bin/activate" on Windows "jylons\bin\activate.bat". For more info on virtualenv see here.

You should now be using the 'jylons' virtual environment. We are close. Type "paster create --t pylons". A script will now guide you through creating your Pylons app. For this part I advise you follow the "getting started" guide from PylonsHQ here.

You'll now need to get the bleeding edge SQLAlchemy, which supports the Jython DBAPI implementation 'zxJDBC'. Use this URL to check it out. Once that is done make sure your jylons virtualenv is activated, if not activate it now. Change dir to the checked out project. Run the file like so: "jython install". This will install the trunk version of SQLAlchemy under the 'jylons' virtualenv.

Before I go I want to make sure that you can connect to your database. In my case Postgres is the database of choice. You could just as well use MySQL. As far as I can tell no integration exists for sqlite, so when you create a new Pylons project that uses SQLAlchemy it won't start up right away; due to errors stemming from sqlite being the configured database. Just switch to MySQL or Postgres.

In the development.ini file you'll want to set the sqlalchemy URL like so:

sqlalchemy.url = postgresql+zxjdbc://<username>:<password>@localhost:5432/<dbname>

Note the '+zxjdbc'. This is what SQLAlchemy 0.6 is using to designate a Jython zxJDBC based DBAPI. If you are interested check the source code for sqlalchemy.engine.url. This will in turn require the Postgres JDBC driver is on the classpath. I hacked it on by updating the jython script to add the driver. Not an optimal solution but workable for now. I'm hoping for a better mechanism to add jar files to Jython's class path at some point.

You should be ready to go. You can now create tables and such in SA. Have fun!


Philip Jenvey said...

You can add jars to sys.path and they'll be available on the classpath. So what I like to do is create a lib/jars/ in my Pylons package, then add all jars inside to sys.path during startup (like in load_environment).


robottaway said...

Thank you. I've used this technique for a bit now, figured I needed to say that!