Slavus programing blog

My random programing (and other) tips.

Interactive Django Shell in Eclipse/PyDev Interactive Console

| Comments

We all know that Django is great Python web framework, however one thing that bothers me is the lack of good development tool (Yes, I know there is VIM, and there is Emacs but guys it is 2008.) In my humble opinion best development tool for python is PyDev, Eclipse plugin for Python. The irony is that the best Python IDE is written in Java :).

With PyDev and Aptana, another irreplaceable plugin(but that is another story), you can do great Django development.

There are lot’s of tutorial how to use Django and PyDev (Configuring PyDev to work with Django : configuring pydev to work with django is a good place to start).

One thing that is very nice in Django is interactive shell, but this feature must be integrated with IDE, I missed this feature for long time, but in version 1.3.15 PyDev released new interactive console, great feature. First you need PyDev version 1.3.15 or higher (try to use latest one).

You can start interactive console from Console View by clicking on the button Open Console(look picture below).

You will get a dialog asking for a type of console you want to open, select Python console and press OK.


That is it, you have now working python interactive console. Out of the box you get some nice features like autocompletition and syntax coloring. What we want do next is to have interactive console for Django.

Here is a little tutorial how take advantage of PyDev interactive console for Djagno shell.

  1. Open Window–>Preferences…
  2. In Preferences Window go to PyDev-> Interactive console

3.In initial interpreter command put following code:

1
2
3
4
5
6
7
8
9
10
import sys
from YOUR_OWN_PROJECT import settings
from django.core.management import execute_manager
execute_manager(settings,['','shell'])

import sys; print '%s %s' % (sys.executable or sys.platform, sys.version)
from django import get_version;print "Django version %s" % get_version()
from django.core.management import execute_manager
from slavus import settings
execute_manager(settings,['','shell'])

Where YOUR_OWN_PROJECT is the name of yours top level Django project. It is important to mention that if you have multiple Python/PyDev projects in your workspace you have to change your preferences configuration every time you want start interactive console for anathor project. After starting the shell, you will get output something like this:

1
2
3
4
5
6
7
8
9
10
11
import sys; print '%s %s' % (sys.executable or sys.platform, sys.version)
C:\Python25\python.exe 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)]
from django import get_version;print "\Django version %s" % get_version()
\Django version 0.97-pre-SVN-7568
from django.core.management import execute_manager
from slavus import settings
execute_manager(settings,['','shell'])
>>> Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>

That is it, you have working Django interactive console. Now you can do things like this:

1
2
3
from slavus.article.models import Article
Article.objects.all()
[<Article: example1>,  <Article: example2>,  <Article: example3>]

But there is also autocompletition:


Enjoy!

Comments