make bpython use Python 3
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.
bpython config file for light background
Problem
The default color scheme of bpython is designed for dark background. If you launch bpython with light background, it’s almost unreadable. Is there a color scheme for light background?
Solution
Create the dir ~/.bpython
and add the following files:
~/.bpython/config
:
# This is a standard python config file # Valid values can be True, False, integer numbers, strings # By default bpython will look for ~/.bpython/config or you can specify a file # with the -c option on the command line # General section tag [general] # Display the autocomplete list as you type (default: True). # When this is off, you can hit tab to see the suggestions. auto_display_list = True # Syntax highlighting as you type (default: True). syntax = True # Display the arg spec (list of arguments) for callables, # when possible (default: True). arg_spec = True # History file (default: ~/.pythonhist): hist_file = ~/.pythonhist # Number of lines to store in history (set to 0 to disable) (default: 100): hist_len = 100 # Soft tab size (default: 4, see pep-8): tab_length = 4 # Color schemes should be put in ~/.bpython/ # e.g. to use the theme ~/.bpython/foo.theme set color_scheme = foo # Leave blank or set to "default" to use the default theme #color_scheme = default color_scheme = jabba [keyboard] pastebin = F8 save = C-s
~/.bpython/jabba.theme
(derived from light.theme
):
# Each letter represents a colour marker: # k, r, g, y, b, m, c, w, d # which stands for: # blacK, Red, Green, Yellow, Blue, Magenta, Cyan, White, Default # Capital letters represent bold # Copy to ~/.bpython/foo.theme and set "color_scheme = foo" in # ~/.bpython/config [syntax] keyword = B name = r comment = b string = g error = r number = B operator = b paren = b punctuation = b token = g [interface] # XXX: gnome-terminal appears to be braindead. The cursor will disappear unless # you set the background colour to "d". background = d output = b main = b prompt = b prompt_more = g
Update (20160214)
As it was pointed out by Leonardo in a comment (thanks!), “you need to put the configuration files inside ~/.config/bpython
instead of ~/.bpython
.” Yeah, applications in newer Linux versions prefer the ~/.config
folder.
Add History and Tab Completion to the Default Python Shell
Problem
You want history and TAB completion in your default Python shell. You are aware of bpython and ipython, but you miss this functionality in the default shell.
Solution
Edit ~/.bashrc
:
# ~/.bashrc export PYTHONSTARTUP=$HOME/.pythonstartup.py
Download pythonstartup.py
from here and rename it to ~/.pythonstartup.py
.
the bpython interpreter
“bpython is a fancy interface to the Python interpreter for Unix-like operating systems (I hear it works fine on OS X). It is released under the MIT License. It has the following features:
- In-line syntax highlighting.
- Readline-like autocomplete with suggestions displayed as you type.
- Expected parameter list for any Python function.
- “Rewind” function to pop the last line of code from memory and re-evaluate.
- Send the code you’ve entered off to a pastebin.
- Save the code you’ve entered to a file.
- Auto-indentation.
- Python 3 support.
For more, see our about-page or just skip right to the screenshots.”
Installation
sudo apt-get install bpython
Tip: add the following line to your ~/.bashrc
file:
alias bpy='bpython'
Then you can start the bpython interpreter with “p
“. Short and simple :)
Alternatives
- classic “python” interpreter
- ipython
You must be logged in to post a comment.