How to get BPython working with Django

bpython is a fancy interface to the Python interpreter for Unix-like operating systems -- www.bpython-interpreter.org

I usually used interactive Python shell, but now so in love with BPython. It works very well on Mac OS X .

At first, I couldn't get Django's "python manage.py shell" to work with Bpython. I can only make it work with IPython.

But I want to use BPython so bad, so let's try to make it work with Django Environment!!

My goal is to run "bpython" in any Django project working directory and get BPython interactive shell running in Django environment. However, running "bpython" in other directorys should behave normally.

Before I made it work, I tried to run some code that relates to Django environment, to see how things fail.


In the picture below, I tried to print out "settings.INSTALLED_APPS". An error message showed up that Django environment has not been set

To fix this, first of all, you have to export the variable PYTHONSTARTUP to the system.

On Mac OS X, do it in ~/.bash_profile
on Linux, if ~/.bash_profile does not exist use ~/.profile

export PYTHONSTARTUP=~/.pythonrc

Add these lines to ~/.pythonrc

try:
     from django.core.management import setup_environ
     import settings
     setup_environ(settings)
except:
    pass
 

Make sure that you append a blank line at the end. (or it raises error like one that Ryan found)

Now load the value that you've just set -- PYTHONSTARTUP. Reopen the shell or if you don't want to exit the shell:

chanita@proteus-tech:~/testbpython$ source ~/.bash_profile


Check check check.

Voila!! It works. :D

Comments:

Thanks, nice tip.

Thanks for introducing me to bpython.

Thanks for liking bpython. :)

And thanks for sharing the knowledge. I'm not a Django user myself but I hear a lot of people want to use bpython with Django, so I'll see about getting this information onto the bpython website or something like that.

I get:

Traceback (most recent call last):
File "/usr/bin/bpython", line 2211, in main
o = curses.wrapper(main_curses, exec_args, options.interactive)
File "/usr/local/lib/python2.5/curses/wrapper.py", line 44, in wrapper
return func(stdscr, *args, **kwds)
File "/usr/bin/bpython", line 2149, in main_curses
repl.repl()
File "/usr/bin/bpython", line 1200, in repl
code_obj = compile(f.read(), filename, 'exec')
File "/home/ryan/.pythonrc", line 6
pass
^
SyntaxError: invalid syntax

Any suggestions?

(Sorry, couldn't figure out how to format code in your comment form.)

Thanks for liking bpython this much. We try to put some work in it.

Bob has asked me to document the procedure you used to make Django work with bpython. Is it okay if I copy/paste the whole thing and refer to this website or would you like me to handle it otherwise?

Please let me know at my email address (simon@ikanobori.jp).

Thanks for the bpython / django solution. There is a ticket in django right now on getting the two working together. The solution in the ticket only works with the latest version of bpython in the repository.

http://code.djangoproject.com/ticket/11542

Ryan checks that you have 4 spaces before "pass". If it doesn't fix see if you miss ":" at the end of the line above "pass".

:)

Hi Simon,
Thanks so much. Feel free to duplicate my content and refer to the website :D.
I'd love to share..

Hey Ryan,

I've figured out why it raised error.

Add a line after "pass", problem fixed!!
Good luck.

P.s. Did you use Joe's own editor("http://joe-editor.sourceforge.net/") ?

Thank you very much for the excellent and useful subject.

Nice, I linked to this, so nice to have bpython with settings and it works with virtualenv too.

Post your comment:

Author:

  • chanita

Archives:

Powered by Django.