Archive

Archive for October, 2014

make bpython use Python 3

October 14, 2014 1 comment

Problem
On my Linux box the default Python is version 2.7. I installed bpython system-wide, thus when I launch bpython, it uses Python 2.7.

How could I have another bpython that uses Python 3? I would like to have both though. Sometimes I need Python 2, sometimes Python 3, and I would like to use the bpython shell with them.

Solution
Simply install bpython in a Python 3 virtual environment. That is, create a Python 3 environment, and install bpython in it with pip. This bpython installation will use Python 3. Then use the absolute path of this bpython executable. And no, you don’t have to activate the virt. env. each time you want to use this bpython.

In a previous post I showed how to patch virtualenvwrapper to make it suitable for creating not only Python 2 but Python 3 environments too. Using this patched virtualenvwrapper, the process looked like this on my machine:

$ cd
$ mkdir bpython_python3
$ cd bpython_python3
$ mkvirtualenv bpython_python3  # creates a Python 3 virt. env.
(bpython_python3)$ pip install bpython
(bpython_python3)$ which bpython
/home/jabba/.virtualenvs/bpython_python3/bin/bpython  # sample; will be different in your case
(bpython_python3)$ deactivate
$

I suggest adding the following aliases to your $HOME/.bashrc:

alias bpy='bpython'
alias bpy3='/home/jabba/.virtualenvs/bpython_python3/bin/bpython'

Of course, you need to customize the path for bpy3.

Now “bpy” launches bpython with Python 2, while “bpy3” starts bpython with Python 3.

Categories: ubuntu Tags: ,