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


    ***NOTE***
    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.

    ***NOTE***
    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.

7 comments:

Rik Brown said...

Hi there,

Stumbled across your blog trying to get a Django-based module I'm using working under Jython.
It's been very useful, but I'm at this roadblock:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/rik/Library/jython2.5.1/Lib/site-packages/ConceptNet-4.0rc1-py2.5.egg/csc/conceptnet4/models.py", line 1, in <module>
from csc.conceptnet.models import *
File "/Users/rik/Library/jython2.5.1/Lib/site-packages/ConceptNet-4.0rc1-py2.5.egg/csc/conceptnet/models.py", line 2, in <module>
from django.db import models
File "/Users/rik/Library/jython2.5.1/Lib/site-packages/django/db/__init__.py", line 50, in <module>
connection = backend.DatabaseWrapper({
File "/Users/rik/Library/jython2.5.1/Lib/site-packages/doj/backends/zxjdbc/sqlite3/base.py", line 106, in __init__
self.client = DatabaseClient()
TypeError: __init__() takes exactly 2 arguments (1 given)


Any idea what could be going wrong here? I know little about Python so I'm not sure where to begin understanding that.

(conceptnet is the module I'm trying to use, I've altered it's default_db_config.py to use the correct backend and it looks like that's working...)

Followed your instructions except the JYTHON_PATH stuff as that didn't seem to be required (i.e. django, django-python ("doj") were already in the sys.path).

Thanks,
Rik.

123 123 said...
This comment has been removed by a blog administrator.
copiesofcopies said...

Hey Matthew, I'm trying to follow your instructions for postgresql instead of sqlite (I think it's essentially the same process), but when I syncdb I always get this error:

zxJDBC.DatabaseError: driver [org.postgresql.Driver] not found

Have you run into this? Any idea what I'm doing wrong?

Matthew Harrison said...

@copiesofcopies
It sounds like you don't have the python/Java bindings for postgresql installed.

Generic Viagra said...

Thanks, I think that it is quite useful and also I share the same feeling with windows users.

vernomcrp said...

thank for wrote this article, i using with sqlite3 , And get error after syncdb, Donno how to go on with trying to display admin page

houston hotels downtown said...

So inspiring. They are truly very well written, precise and to the point. Take care :D.