lakshman
Comments
Reactions

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

By : lakshman

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


Can we help you build amazing apps? Contact us today.

Comments

Leo Shklovskii

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.

commmenttor
shabda

@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.

commmenttor
codekoala

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!

commmenttor
YHVH

"seems to claim to obtain" is unnecessary, "claims to obtain"

commmenttor
yTim 12th Dec., 2009

When you will provide answers for djagno quiz ??

commmenttor
Confluence: TeacherLine

Courses...

PBS TeacherLine Courses are created as a set of HTML, CSS, JS and media files that constitute an entire course. All required files for a course are stored in a single course repository, which is currently managed through a GIT server.......

commmenttor
Bob Farrell

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

commmenttor
Suno Ano

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 ;-)

commmenttor
I love django 17th Jan., 2010

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

commmenttor
Andy

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.

commmenttor
Face Ajan

Facebook Şifre Alma Programı
Bilgisayara Girme Programı

commmenttor
China

Help, I've been informed and I can't boemce ignorant.

commmenttor
xruecze

h6TrA4 clirmmppfjpi

commmenttor
xwoxfgdj

q6cx8T xvsxzwblxpdk

commmenttor
Ronan Jouchet

Hi! From what I understanding, things improved in Django releases 1.3 and 1.5. Maybe you coud mention https://docs.djangoproject.com/en/1.5/ref/django-admin/#shell in your post? To quote it here,

"""
Django will use IPython or bpython if either is installed. If you have a rich shell installed but want to force use of the “plain” Python interpreter, use the --plain option, like so:
django-admin.py shell --plain

Changed in Django 1.5.

If you would like to specify either IPython or bpython as your interpreter if you have both installed you can specify an alternative interpreter interface with the -i or --interface options like so:

IPython:
django-admin.py shell -i ipython
django-admin.py shell --interface ipython

bpython:
django-admin.py shell -i bpython
django-admin.py shell --interface bpython
"""

commmenttor

Reactions

uswaretech_blog

Using bpython shell with django (and some Ipython features you should know): What is bpython?

bpython is a f.. http://bit.ly/53RAvw

This comment was originally posted on Twitter

loggly

Fancy shmancy shell for Python: http://bit.ly/5Aemse #python #django #shell

This comment was originally posted on Twitter

hkrnws

Using bpython shell with django (and some Ipython features you should know) http://bit.ly/8mSENo

This comment was originally posted on Twitter

pythonism

http://twitter.com/loggly/statuses/6604797701 “Fancy shmancy shell for Python: http://bit.ly/5Aemse #python #django #shell”

This comment was originally posted on Twitter

YCHackerNews

Using bpython shell with django (and some Ipython features you should know): http://bit.ly/5MewXg Comments: http://bit.ly/62i7Kf

This comment was originally posted on Twitter

tudorizer

Shabda Raaj: Using bpython shell with django (and some Ipython features you should know) http://tinyurl.com/ybf2kls

This comment was originally posted on Twitter

riobard

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

This comment was originally posted on Hacker News

uswaretech

http://bit.ly/4X0HR3 Using bpython shell with django (and some Ipython features you should know)

This comment was originally posted on Twitter

hrjn_rss

Using bpython shell with django (and some Ipython features you should know): Comments http://url4.eu/v4LY

This comment was originally posted on Twitter

WebStartupGroup

StartupNews: Using bpython shell with django (and some Ipython features you should know) http://bit.ly/5BL7L1

This comment was originally posted on Twitter

AtlantaWebsite

Using bpython shell with django (and some Ipython features you…:  Bookmark this on Delicious – Saved by zchro… http://bit.ly/6j9kzh

This comment was originally posted on Twitter

djangostories

Using bpython shell with django (and some Ipython features you should know)
: http://bit.ly/5TsHdO

This comment was originally posted on Twitter

djangostories

Shabda Raaj: Using bpython shell with django (and some Ipython features you should know)
: http://bit.ly/72v7og

This comment was originally posted on Twitter

newsycombinator

Using bpython shell with django (and some Ipython features you should know) http://bit.ly/5SmUYX

This comment was originally posted on Twitter

scorpion032

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

This comment was originally posted on Hacker News

tek_news

Reddit/p: Using bpython shell with django (and some Ipython features you should know) http://bit.ly/6Sx1ef

This comment was originally posted on Twitter

salve

Using bpython shell with django (and some Ipython features you should know) — The Usware Blog – Django Web Develo… http://bit.ly/6j9kzh

This comment was originally posted on Twitter

scorpion032

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

This comment was originally posted on Hacker News

etal

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

maguro7

Using bpython shell with django (and some Ipython features you should know) ? The Usware Blog – Django Web Developm… http://bit.ly/6j9kzh

This comment was originally posted on Twitter

torabutter02_fd

【Mix】 Using bpython shell with django (and some Ipython features you should know) ? The Usware Blog – .. http://bit.ly/7PBBUq

This comment was originally posted on Twitter

buck__mulligan

Using bpython shell with django (and some Ipython features you should know) — The Usware Blog – Django Web Deve.. http://bit.ly/7PBBUq

This comment was originally posted on Twitter

riobard

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

ez77

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

ez77

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

DeliciousHot

Using bpython shell with django (and some Ipython features you should know) — The Usware Blog – Django Web Development http://is.gd/5lm7b

This comment was originally posted on Twitter

DeliciousHot

Using bpython shell with django (and some Ipython features you should know) — The Usware Blog – Django Web Development http://is.gd/5lm7b

This comment was originally posted on Twitter

ionrock

Seriously… thi might very well be the coolest things I’v found
recently IPShellEmbed
http://bit.ly/7g9uwr

This comment was originally posted on Twitter

UloPe

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

djangostories

Using bpython shell with django (and some Ipython features you should know)
: http://bit.ly/4PGSJA

This comment was originally posted on Twitter

coulix

Using bpython shell with django , improved Ipython shell : http://bit.ly/6UzRIo auto

This comment was originally posted on Twitter

Fry3001

bpython: noch eine klasse python shell http://is.gd/5lKcb

This comment was originally posted on Twitter

pythonism

http://twitter.com/Fry3001/statuses/6627536686 “bpython: noch eine klasse python shell http://is.gd/5lKcb”

This comment was originally posted on Twitter

pythonism

http://twitter.com/Fry3001/statuses/6627536686 “bpython: noch eine klasse python shell http://is.gd/5lKcb”;

This comment was originally posted on Twitter

pythonism

http://twitter.com/daveaitel/statuses/6638228584 “The ide in a python >>> : http://bit.ly/7g9uwr”;

This comment was originally posted on Twitter

pythonism

http://twitter.com/daveaitel/statuses/6638228584 “The ide in a python >>> : http://bit.ly/7g9uwr”

This comment was originally posted on Twitter

pr_entrepreneur

Using bpython shell with django (and some Ipython featu… http://bit.ly/6K5z5E #postrank #entrepreneur

This comment was originally posted on Twitter

delicious50

Using bpython shell with django (and some Ipython features you should know) — The Usware Blog – Django Web Development http://bit.ly/4vzvkV

This comment was originally posted on Twitter

scorpion032

This is pretty cool!

This comment was originally posted on Reddit

ionel_mc

Using bpython shell with django (and some Ipython features you should know) — The Usware Blog – Django Web Deve.. http://bit.ly/6j9kzh

This comment was originally posted on Twitter

thoas

Using bpython shell with #django http://bit.ly/87u1sA, really nice feature #python

This comment was originally posted on Twitter

DoYouDjango

Using bpython shell with django (and some Ipython features you should know) – http://bit.ly/52y8vD

This comment was originally posted on Twitter

oliverandrich

bpython + django = ♥ Gefällt mir besser als shell_plus. http://bit.ly/7dByod Meine .pythonrc: http://gist.github.com/265344

This comment was originally posted on Twitter

© Agiliq, 2009-2012