Using bpython shell with django (and some Ipython features you should know)

by lakshman on December 12, 2009

What is bpython?

bpython is a fancy interface to the Python interpreter for Unix-like operating system.

says the bpython home page. It provides syntax highlighting, auto completion, auto-indentation and such stuff.

Unlike iPython, which implements then entire shell functions and emulates the standard python shell, and adds enhancements, bpython just adds features on top of the existing python shell functionality, using the curses module.

bpython

The “killer feature” of bpython is, as you can see from the image above, the IDE like prompting of the function parameters and the doc string of the function dynamically. I have always thought, what IntellijIDEA is to Java, IPython is to Python*. But bpython makes it more so, in terms of the code completion, which adds a lot of value to a ramping up developer.

The only Python IDE that provides Source Assistant and Go-To-Source functionality conveniently, is the commercial one, Wing IDE Professional. Even with that, since all the defined models are replaced (by using meta-classes) in the runtime, that source assistant is not perfect. Newer versions of Wing seems to claim to obtain data dynamically at the runtime, but its not always comfortable to write code, keeping the IDE in a breakpoint. But hey, bpython provides all these for free!

But I already use Ipython

You do? So do I. In fact, I am a Power Ipython user, I use all kinds of %magic functions: %whos, %logstart, %bookmark, %ed and sometimes use Ipython as an alternative even to bash: %cd, %ls, %ll. And even for doc testing: %doctest_mode, copy-pasting %hist -n or running some code module in the interpreter namespace: %run -i and searching code %psearch. You can also give any arbitrary bash(or your default shell) command by escaping it with !. Oh, ?, ?? and / are of course the starters.

In fact did you know, you could get to ipython shell within any arbitrary place within your django code? Just use the following:

from IPython.Shell import IPShellEmbed
ipython = IPShellEmbed()

and then call ipython() anywhere within your view, model, forms and you will be dropped to the shell. Its like ipdb.set_trace() only better (unless you want to step through, that is).

The Python readline bug

The Python version 2.6.4 (the version that is shipped with Ubuntu 9.10), introduced a readline bug that adds spaces after tab completion. This bug also affects the Ipython, as it uses the same readline module. If you spend a lot of time on the shell (if you use python, you must, right?), it is very annoying to backspace after each tab, all the time.

Although the bug got fixed pretty soon, it hasn’t yet made it to any release that ubuntu updates to. There are ways to workaround this problem, by fixing it at the python level and at the Ipython level, many of them are discussed on the corresponding Ipython bug

Using bpython with django

Now that you want to use bpython with django, either because you like the auto completion, or because you find the read line bug annoying (and don’t want/care-enough to patch your python locally), or you just want to try it, how to do it?

django manage.py shell unfortunately, drops only into the ipython shell (if you have ipython installed), and there is no straight forward way to get it to drop to bpython.

But there is still a way to use bpython with django. Just modify your ~/.bashrc to define a python startup environment variable

export PYTHONSTARTUP=~/.pythonrc

And within it, setup the django environment, that is, do it here manually the thing that python manage.py shell would do for you:

#.pythonrc
try:
    from django.core.management import setup_environ
    import settings
    setup_environ(settings)
    print 'imported django settings'
except:
    pass

This way, bpython or even just python on the command line, should import the django environment for you.

Importing all models automatically into the shell

But then, if you are possibly already used to python manage.py shell_plus (If you are not, you should be.) that is defined by django_command_extensions.

So while we are at setting up the django environment, lets just also import all the models into the shell namespace, so that it is convenient to work. Following is some ugly quick hack to get it done.

This can of course be improved upon. If you do it, just leave it in the comments! In fact it would be good if it also includes from startup.py import * in a try catch, so that, you can put some extra code into startup.py. Saving into startup.py from within a bpython shell is just a Ctrl+s anyway. That way each time you get to the shell, you can have the same expected environment; and it is quite easy to change that file.

*I know, I know, IPython doesn’t refactor code, nor build, nor does a million things that Intellij does, but Python is not Java and basically both intend to enhance developer productivity. And I spend a lot of time on the IPython shell and find it to be a tool just like Intellij is a tool, for Java.

Related posts:

  1. Better Python package management using source and version control systems
  2. Tools of Pro Django developer – aka What powers dinette and almost every app we write.

2 Comments 39 Tweets 7 Comments 2 Other Comments

{ 4 trackbacks }

Elsewhere, on December 13th - Once a nomad, always a nomad
December 13, 2009 at 2:54 pm
Confluence: TeacherLine
December 14, 2009 at 2:53 pm
Using bpython with django : Not My Idea — Snippets
December 22, 2009 at 8:01 am
Usando bpython en django | malev's blog
June 16, 2010 at 9:54 pm

{ 57 comments… read them below or add one }

1 Leo Shklovskii December 12, 2009 at 11:54 am

PyDev gives you quite a bit of the functionality that Intellij does – it has Source Assistant and Go-To-Source as well as other great features and its completely free.

2 shabda December 12, 2009 at 12:08 pm

@Leo

I love pytyhon, but the state of IDEs in python is at least a generation behing Java/(or Visual studio). eg In Eclipse/NetBeans, I almos never have to import, Ctrl+shift+i, and I am done.

3 codekoala December 12, 2009 at 12:31 pm

Thanks for this article! I had never heard of bpython, but I do use ipython quite a bit. I’ll have to try this out!

4 YHVH December 12, 2009 at 12:39 pm

“seems to claim to obtain” is unnecessary, “claims to obtain”

5 yTim December 12, 2009 at 6:29 pm

When you will provide answers for djagno quiz ??

6 Ulrich Petri December 12, 2009 at 10:17 pm
7 Bob Farrell December 23, 2009 at 3:41 pm

Thanks for the post, seems like the site’s getting a bit of traffic from here, now I am even more famous, hooray !

8 Suno Ano December 29, 2009 at 4:08 am

I am totally guilty of having stolen this bits and pieces and also put them here http://sunoano.name/ws/public_xhtml/python.html#is_there_a_better_python_shell_interpreter

greetings to all Pythonistas our there ;-)

9 I love django January 17, 2010 at 9:52 am

Simple way to use bpython with django in only one command line http://bit.ly/4A09Tb

10 Andy February 5, 2010 at 11:07 am

Thanks for your post, this bpython is just The Thing. Not only is IPython a bit slow, it also has a nasty bug with non-ascii (at least cyrillic) characters. The ticket is full of desperate cries and simple patches, but these patches seem to stay there forever. The extra space issue was also very annoying (sometimes it was OK not to hit backspace, but usually that would break further completion). And the prompting of parameters is simply what was missing in all tools I tried — except for the bulky IDEs which usually stay in your way instead of helping with certain tasks.

11 Face Ajan May 8, 2010 at 4:34 pm
20 riobard December 12, 2009 at 1:11 pm

bpython seems to be much better than ipython … switching now ~

This comment was originally posted on Hacker News

28 scorpion032 December 12, 2009 at 4:43 pm

That is true only if you don’t use the magic functions!

This comment was originally posted on Hacker News

29 scorpion032 December 12, 2009 at 3:45 pm

And it is true that most people don’t.I think both have their place.

This comment was originally posted on Hacker News

31 etal December 12, 2009 at 5:05 pm

Well, depending on what you want from it — the curses UI and autocomplete implementation are very nice, but ipython has a lot of additional features that bpython isn’t interested in implemented. I miss the ??function_name feature in ipython that shows the complete source code for a function or class, since the function header isn’t always informative enough; I tend to use ipython’s bash emulation pretty frequently too.

This comment was originally posted on Hacker News

35 riobard December 12, 2009 at 7:24 pm

I just need a better replacement for the default python shell. iPython has those nice extras, but I rarely use them … so bpython is a much better choice for me now :)

This comment was originally posted on Hacker News

36 ez77 December 12, 2009 at 7:26 pm

Can anyone tell me why the following only outputs the exit status "0"?import osos.system(’ls’)

I don’t get this problem in ipython.

Thanks

This comment was originally posted on Hacker News

37 ez77 December 12, 2009 at 7:26 pm

Can anyone tell me why the following only outputs the exit status "0"?import osos.system(‘ls’)

I don’t get this problem in ipython.

Thanks

This comment was originally posted on Hacker News

41 UloPe December 12, 2009 at 10:15 pm

Also I have a branch on github that adds bpython support to shell_plus: http://github.com/ulope/django-extensions/commit/1fb163c72017ea889ce8c6aa61160a4bbf5d766c

This comment was originally posted on Reddit

Leave a Comment

Additional comments powered by BackType

Previous post:

Next post: