Tuesday, December 9, 2008

Setting up Django on Jython with sqlite

After spending a night of frustration trying to get Django running under Jython and using sqlite3, I finally figured it all out. Here's the short version of what you need to do on a linux-y system (Windows users are on their own):

  1. Have the latest django from svn.
    $ svn co http://code.djangoproject.com/svn/django/trunk/ django-trunk

    If you haven't already done so, continue setting up as per the instructions at Django's site.

  2. Get the latest jython from svn. Pre 2.5 will NOT work.
    $ svn co https://jython.svn.sourceforge.net/svnroot/jython/trunk/ jython
    $ cd jython
    $ ant
    $ export PATH=$PATH:`pwd`/dist/bin

  3. Get django-jython from svn.

    $ svn co http://django-jython.googlecode.com/svn/trunk/ django-jython
    $ cd django-jython
    $ jython setup.py install

  4. This was the part that caused most of the headaches, getting sqlite to work.
    Grab SQLiteJDBC.
    Add the .jar to your $CLASSPATH.

    $ export CLASSPATH=$CLASSPATH:/path/to/jar/sqlitejdbc-v###.jar

  5. Add Django to your $JYTHONPATH.

    $ export JYTHONPATH=$JYTHONPATH:/path/to/your/python/libs/site-packages/django

    On OS X, this is /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django .

  6. Add django-jython to $JYTHONPATH.

    $ export JYTHONPATH=$JYTHONPATH:/path/to/django-jython

  7. You should be able to create new Django projects with

    $ jython django-admin.py startproject projname

  8. Edit your mydjangoproject/settings.py to include the sqlite3 database backend provded by django-jython.

    DATABASE_ENGINE = 'doj.backends.zxjdbc.sqlite3'

  9. Syncdb and runserver.

    $ cd mydjangoproject
    $ jython manage.py syncdb
    $ jython manage.py runserver

    syncdb outputs a big nasty error for me. This is an issue with jython and developers are looking at it now. My project still seems to work after this error, but don't use this for any important data because it's not guaranteed to work.

    When starting your project, you may encounter the following error:

    Error: Could not import settings 'mydjangoproject.settings$py' (Is it on sys.path? Does it have syntax errors?): No module named settings$py

    This is because jython generates a "settings$py.class" when it compiles it, and manage.py loads this thinking it's settings.py. You can delete settings$py.class each time before you attempt to use manage.py, or apply this patch by Frank Wierzbicki.

I'm writing this entirely from memory, so I almost certainly have forgotten some steps. Please mention any trouble you have via comments to this blog post so I can update my post accordingly.