Login or Sign up

using bpython with django and pinax

Posted by: skyl on Jan. 16, 2010

I found this nice article about how to run bpython with Django. Running with pinax takes just a little extra. Pinax does some path-hacking in the manage.py script. This allows pinax to import from the yourproject/apps directory and pinax/apps directories.

I only know linux; maybe somebody else could kick in the other OSes.

Edit your .bashrc to include:

export PYTHONSTARTUP=~/.pythonrc

Then, in this .pythonrc file include something like:

try:
    from django.core.management import setup_environ
    import settings
    setup_environ(settings)

    if settings.PINAX_ROOT:
        import sys
        from os.path import join
        sys.path.insert(0, join(settings.PINAX_ROOT, "apps"))
        sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))

except:
    pass

A bare except is terrible practice and so is path-hacking. But, in this case, it does exactly what we want. If we are not in a directory with a settings module then we would get an ImportError (or if settings imported but was something unexpected we would get a different exception). If Django and Pinax are present, then we get the settings and the apps. If Django is present and Pinax is not then we get the environ setup. If Django is not present, or there is another problem, just start bpython normally.

Make sure that you have bpython installed in the virtualenv:

(pinax-env)$ pip install bpython

Now you should be able to run bpython from your project directory and these apps modules will be on the path as well as all of the model objects, etc.

>>> import account
>>> account.__file__
'/home/skyl/.../pinax/pinax/apps/account/__init__.pyc'
>>> from account.models import Account
>>> a = Account.objects.all()
>>> a.
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│aggregate          all                annotate           complex_filter     count
│create             dates              db                 defer              delete
│distinct           dup_select_related exclude            exists             extra
│filter             get                get_or_create      in_bulk            iterator
│latest             model              none               only               order_by
│ordered            query              reverse            select_related     update
│using              value_annotation   values             values_list
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

Comments on This Post:

Please Login (or Sign Up) to leave a comment