famfop famfop - 5 months ago 8
Bash Question

Python configuration of interactive shell

I'm trying to find out how to configure the "python interactive shell" on linux (i.e. the program executed when typing

in terminal). I'm looking forward to configure stuff such as coloring ps[12], tab completion (?), etc.

I'm aware of ipython/jupyter, bpython, etc. but I don't want fancy (unnecessary?) stuff but only some colored terminal :)

Is it possible to have a kind of config file?


There is an environment variable you can set, PYTHONPATH:

If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session. You can also change the prompts sys.ps1 and sys.ps2 and the hook sys.__interactivehook__ in this file.

To set the environment variable, go to your terminal and type:

$ export PYTHONPATH=/path/to/my/python/file.py

($ is the prompt, not something you should type.)

Since that won't last beyond the current session, you probably want to put it in your .bashrc file.

In your /path/to/my/file.py you can do whatever Python stuff you want. For example, you might want to have the primary and secondary prompts to be green:

import sys

color = "\x1b[32m{}\x1b[m"
sys.ps1 = color.format(sys.ps1)
sys.ps2 = color.format(sys.ps2)

# We don't want variables to be around in our interactive sessions.
del color
del sys

You can do whatever you want in there. In mine, I set a history file that is written to atexit. That way, Up can go beyond the current session. I also added tab completion.