Python Interactive Shell Interfaces

serhatteker

Serhat Teker

Posted on March 21, 2022

Python Interactive Shell Interfaces

The Python Interactive Shell is an interactive interpreter that can execute Python commands.

You can invoke it from your default shell — zsh, bash, fish etc.:

$ python
# or for a specific python version
$ python3.9
Enter fullscreen mode Exit fullscreen mode

Also you can run interactive shell with a module/file like below:

$ python -i somefile.py
Enter fullscreen mode Exit fullscreen mode

Use Case

Thanks python, however I can't say I enjoy your default shell. I need more capable, colorful interactive shell like IPython, bpython.

It's easy to achieve this in Django. It's well documented in django-shell:

# IPython
$ django-admin -i ipython
# bpython
$ django-admin -i bpython
Enter fullscreen mode Exit fullscreen mode

Well, how can we achieve it in Python?

Solution

Python's way is kind of similar; you invoke it with some module using -m flag:

For IPython run:

$ python -m IPython -i
# with module/file
$ python -m IPython -i somefile.py
Enter fullscreen mode Exit fullscreen mode

For bpython run:

$ python -m bpython -i
# with module/file
$ python -m bpython -i somefile.py
Enter fullscreen mode Exit fullscreen mode

NOTE
In order to be able to use IPython and/or bpython first you have to install them:

$ python -m pip install ipython
$ python -m pip install bpython

Yes, it's that easy and now we can take advantage of amazing features like below:

  • In-line syntax highlighting
  • Readline-like auto-complete with suggestions displayed as you type
  • Expected parameter list for any Python function, etc.

bpython-demo-gif

Probably it would be better to have them as aliases:

# .aliases
alias pi='python -m IPython -i'
alias pib='python -m bpython -i'
Enter fullscreen mode Exit fullscreen mode

All done!

💖 💪 🙅 🚩
serhatteker
Serhat Teker

Posted on March 21, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related