I recently set up a new virtualenv for Python 3.5 on Ubuntu 16.04 and whenever I enter a python shell in the terminal (by typing "python3") I have no persistent history of commands I entered in the terminal previously.
On the regular system python3, persistent command history works fine and required no special setup. How can I enable it for the virtualenv?
As the answer is hidden in the comments and therefore hard to find:
From https://unix.stackexchange.com/a/121390 from @Jason's comment:
Create a .pythonrc.py file:
import os
import atexit
import readline
readline_history_file = os.path.join(
os.path.expanduser('~'),
'.python_history'
)
try:
readline.read_history_file(readline_history_file)
except IOError:
pass
atexit.register(readline.write_history_file, readline_history_file)
and export it by adding the following line to your ~/.bashrc:
export PYTHONSTARTUP=$HOME/.pythonrc.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With