Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No persistent command history in Python 3.5 (virtualenv)

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?

like image 372
Jason Avatar asked Aug 11 '26 01:08

Jason


1 Answers

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
like image 162
Martin Avatar answered Aug 12 '26 17:08

Martin