We all know that one of the cool things that Python offers is it’s interactive shell that makes it possible to quickly test things. It’s included in every Python release and can be invoked by simply entering ‘python’ on the command-line. While this is great already, the official interactive shell lacks several features that would be convenient (such as code completion, syntax hilighting, pasteservice support…). IPython is an attempt to provide an interactive python shell with the same capabilities, but additional goodies. I was using it mainly because it featured code completion.

I came to the python support channel today to ask about some strange behavior I had observed. Consider the following snippet:

    In [1]: class Foo(object):
    ...:     def __getattr__(self, attr):
    ...:         print attr
    ...:         
    ...:         

    In [2]: foo = Foo()

    In [3]: foo.asd
    asd
    asd

    In [4]: Foo().asd
    asd

Now this should of course print ‘asd’ once in every case. This is IPython’s fault and seems to be related to this bug report. When asking about this, somebody suggested bpython. I headed over to their page and was quite pleased by what I saw in their video. bpython features syntax hilighting, code completion, expected parameters, rewind and pastebin support. See for yourself:

video
9 comments Jun 2, 2009 4:19:00 PM bpython, coding, planet-ubuntu, python

Comment by Vitaly Babiy — Jun 2, 2009 4:50:44 PM | #- re

Bpython is an awesome way to use the interactive console for python. Has any one been able to get it to work with django shell the way ipython works?

Comment by Jeff Schroeder — Jun 2, 2009 5:23:00 PM | #- re

You can add history and command line completion to the interactive “python” interpreter. It requires 2 things. In you users ~/.bashrc, add a line like this: export PYTHONSTARTUP=~/.pythonrc Then create a ~/.pythonrc and add this:

try:
    import readline
except ImportError:
    pass
else:
    import os
    import atexit
    import rlcompleter

class irlcompleter(rlcompleter.Completer):
    def complete(self, text, state):
        if text == "": 
            readline.insert_text('\t')
            return None
        else:
            return rlcompleter.Completer.complete(self,text,state)

# You could change this line to bind another key instead tab.
readline.parse_and_bind("tab: complete")
readline.set_completer(irlcompleter().complete)
# Restore our command-line history, and save it when Python exits.
historyPath = os.path.expanduser("~/.pyhistory")
This gives you tab based command completion and history support. — Jeff Schroeder http://www.digitalprognosis.com

Comment by Jeff Schroeder — Jun 2, 2009 5:40:07 PM | #- re

Your blog software destroyed the code in the previous post. I’ve went ahead and uploaded it for you here: www.digitalprognosis.com/opensource/pythonrc

Comment by dennda — Jun 2, 2009 6:54:01 PM | #- re

Jeff, thanks for the hint. (I wrapped your code with the appropriate code-tags so it’s displayed correctly now.)

Comment by oliver — Jun 2, 2009 9:07:07 PM | #- re

bpython looks very interesting! Does anyone know if there’s a package for Jaunty?

Comment by Pierre Relation — Jul 2, 2009 7:42:03 AM | #- re

This is a very useful addition to my Python toolbox.

Comment by Laura Incontri — Jul 14, 2009 4:57:00 PM | #- re

Actually i am pretty new in this world…but I have to say that Bpython is something awesome, especially if you use the interactive console for python. To Oliver: I think there is a package for Jauntry, I don’t know where, but I read something about that.

Comment by Ambyr Amoureuse — Jul 17, 2009 1:19:00 PM | #- re

I know so many programming languages but the Python is the most difficult for me, I just can’t get it :) I try it to learn it alone but may be I need some lessons. Good for you that you did it :)

Comment by MattB — Sep 15, 2009 8:08:31 PM | #- re

You can get bpython working with django by using the patches that were just posted on this ticket: code.djangoproject.com/ticket/11542

Copy either bshell.py or shell.py into your django/core/management/commands/ folder.