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:
| 9 comments | Jun 2, 2009 4:19:00 PM | bpython, coding, planet-ubuntu, python |
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?
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")
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
Jeff, thanks for the hint. (I wrapped your code with the appropriate code-tags so it’s displayed correctly now.)
bpython looks very interesting! Does anyone know if there’s a package for Jaunty?
This is a very useful addition to my Python toolbox.
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.
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 :)
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.