EngineeringFantasy

Watch out iPython here comes b(i)python!

Tuesday, 20 May 2014

Like what you see?

So if you've been using Python for some time now, you've probably heard of iPython. This is pretty much the go to shell for most pythonistas who want to get a better command line debug experience. I find ipdb to be a godsend when I'm quickly trying to diagnose the problem.

But what about when you're trying to explore a library or even write some code quickly with plenty of auto-completion, or perhaps quickly demonstrate something, you might want to chose bpython. And if you wanted the best of both worlds, both ipython and bpython, there's bipython. But first, lets take a look at what bpython can offer us:

Getting Started

bpython works on *nix systems with ease, for windows users all you need to is use this installer to install the package. But *nix users, its much simpler:

pip install bpython

In my tests bpython worked with both python 2 and python 3 as well as pypy because its written in pure python and the only dependency bpython has is on pygments:

bpython works on Python 3!

Once you've installed bpython, you can then just involve it from the command line:

How you start it :)

A Few Quick Tips

  • The first thing I learn to do with any shell or any new software for that matter is how to get out out of it. The best way to get out of bpython is just to hit CTRL + D, and that will take you out.
  • bpython will automatically add the contents of your session to your console.
  • You can save you session with CTRL + S.
  • You can clear the bpython screen with CTRL + L.

Sweet code completion

I love code completion. How can you not love something that pretty much writes your code for you? As a person who's always looking out for better tools, bpython hits the spot. It's got the best code completion I've seen in any shell, it even puts fish to shame:

What I love about bpython is how fast the code completion pops up and the way documentation just pops up when you want to use a function.

Don't worry if you mess up

When I'm trying to reproduce something on the command line, I often make mistakes (although with bpython's code completion it's a lot harder to do). I'm trying to import asyncio here:

>>> import asycnio
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ImportError: No module named 'asycnio'

Now, when you're trying to send a console session to someone, these mistakes are not only annoying but also can get in the way of the person trying to help you, so you can just redo the last line with CTRL + r:

Please note that this does not negate what you did, it just clears the screen of the last command that you input. So if you imported datetime, but you actually wanted to import requests (yes, I don't know how to spell but bear with me), then datetime will still be usable from the shell:

Share your code on the fly

If you want to share your session with someone, you can do that in just one keystroke F8. It will prompt you for confirmation, after which you can view your paste on bpython's code hosting site:

Pastebin

After you've said yes to the prompt, it will generate a link for you, so this is the one it generated for my session.

Contacting the team

These are just some of the awesome things you can do with bpython. There's probably more. I don't know them all since I'm still exploring what you can do with it.

You can find out more about by talking to the guys on their irc in #bpython on irc.freenode.net. Find out more about the community around bpython here.

I initially though that bpyhon was not properly maintained, but if you take a look at their project page, it seems that its being kept up to date:

Its up to date :)

But what if you wanted both?

Well you'd have to use bipython then. Its still in the works, and there's no proper highlighting for errors right now like in ipython, but its heading towards being a package that unifies both ipython and bpython. So, you need to install ipython, bipython:

pip install ipython bipython

And you're done with the installations but running bipython is not as simple. At this time, bipython does not work with python 3, but it does work with python 2. Have a look:

Update

Last Updated on 15 Nov 2014 1:11 PM

Information about bipython added.