Python Startup
There exists a useful feature for the python interpreter: The PYTHONSTARTUP variable.
It can be set to a python source file that will be executed when python is started in an interactive mode. This can be used to add command line completion and a history.
This needs to be added to ~/.bashrc or any of the startup files you use when starting an interactive bash session:
export PYTHONSTARTUP=${HOME}/.pystartup
The following code will add history as well as the command line completion.
File ~/.pystartup:
import atexit
import os
try:
import readline
except ImportError:
print "Module readline not available."
else:
import rlcompleter
readline.parse_and_bind("tab: complete")
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
atexit.register(save_history)
del os
atexit
readline
rlcompleter
save_history
historyPath
Posted at: 09:28 | Permalink | category: /english/technical