Bpython is an open-source terminal-based fancy Python interpreter which offers important features that are not part of the default interpreter. Some of the core features of bpython include:
- Supports syntax highlighting
- Supports auto-indentation
- Supports external editors
- Easily customizable
- Supports rewind feature
[ You might also like: Pyzo – The Interactive Editor for Scientific Python ]
In this article, we will see how to set up a Bpython Python interpreter in Linux and discuss some of the features it offers.
How to Install Bpython in Linux
The simplest way to get bpython is by installing from the default repositories on Debian, Ubuntu, and Fedora distributions as shown.
$ sudo apt install bpython # Debian based distribution $ sudo pacman -S bpython # Arch based distribution $ sudo dnf install bpython # Fedora distribution
To get the latest bpython, you need to install it using PIP which is a package manager for Python. Check if pip is installed in your machine, if not run the following command based on distribution to install pip.
$ sudo apt install python3-pip # Ubuntu based distribution $ sudo pacman -S python-pip # Arch based distribution $ sudo dnf install python3 python3-wheel # Fedora distribution
There is also a common way to install pip latest version using a bootstrap script. Run the following command which will download and run the script.
$ curl https://bootstrap.pypa.io/get-pip.py -o pip.py $ python3 pip.py
If you have python2 as the default interpreter in your Linux machine then either you have to change the default interpreter to python3 or run the installer script with python3 as a prefix as shown above. If you still try to run with python2 you will get the following error.
ERROR: This script does not work on Python 2.7 The minimum supported Python version is 3.6. Please use https://bootstrap.pypa.io/pip/2.7/get-pip.py instead.
Now you can go ahead and install bpython by running the following pip command.
$ pip install bpython
Bpython will be installed under the home directory local bin. You have to add this directory to the PATH variable if not already added. Open bashrc file.
$ nano ~/.bashrc
and add the following line.
and source bashrc file.
$ source ~/.bashrc
How to Use Bpython in Linux
To launch bpython from the terminal you can run any of the following commands. You can find which version of python is used by bpython from the first line once your start the bpython session as shown in the below image.
$ bpython $ python3 -m bpython $ exit() or CTRL + D # exit from session
F1 inside the bpython shell which will open the help page. This will be very handy if you are a first-time user.
Auto Suggestion and Syntax Highlighting
Bpython offers auto-suggestion and syntax highlighting features. Start typing in the terminal and it will display a list of suggestions as shown in the below image.
$ bpython >>> import numpy
Vanilla Python interpreter will not support auto-indentation. Bpython does a better job by doing proper indentation automatically. Take a look at the below image. When I press enter after def statement it properly places the cursor for the next line.
>>> def hello_world():
There is an option to rewind the code you executed which will pop the last line of code from memory. Press
CTRL + R to rewind.
External Editor Support
Bpython supports external editors to be used for editing history, a block of code, or a current line. The default editor is
vi and you can set different text editors by modifying the config files. Press
F3 to open the configuration file and modify the editor parameter.
# External editor to use for editing the current line, block, or full history # Examples: vi (vim) # code --wait (VS Code) - in VS Code use the command palette to: # Shell Command: Install 'code' command in PATH # atom -nw (Atom) # Default is to try $EDITOR and $VISUAL, then vi - but if you uncomment # the line below that will take precedence # editor = vi
F7 to open the editor.
There are some important Bpython files you should be aware of when working with bpython.
~/.pythonhist - Stores all the code you type in the terminal ~/.config/bpython/config - can customize keys and behavior
You can get the list of keybindings by pressing
F1 (help page). You can also customize the keys by editing ~/.config/bpython/config file.
$ vi ~/.config/bpython/config
That’s it for this article. If you have any feedback use the comment section to share it with us.