orbitalflower

How to disable history on Python 3

Posted in Computing on — last updated

Since Python 2 was deprecated at the beginning of 2020, you may have noticed that the Python 3 interpreter logs a history of all commands to .python_history. This has been enabled by default since Python 3.4 and is remarkably difficult to disable (bug #20866), and the official documentation is not much help.

The following method has been tested in Ubuntu and will prevent the python3 interpreter from creating or updating the .python_history file, but you should retain the command history during a session as normal in Python 2.

Update (2026)

The solution previously described by this article no longer works correctly. The method which appears to work currently is to add this line to .bashrc or .bash_aliases:

export PYTHON_HISTORY=/dev/null

Original method

Step 1: Enable .pythonrc

Your first problem is that the Python interpreter’s settings file is not enabled by default (bug #8919).

In your .bashrc or .bash_aliases file in your homedir, insert the following instruction:

export PYTHONSTARTUP=~/.pythonrc

Step 2: Restart terminal

If you have a terminal window open at this point, you will need to restart it for the new environment variables to take effect.

Step 3: Edit .pythonrc

Create a file called .pythonrc in your homedir containing the following:

import readline
readline.write_history_file = lambda *args: None

Credit to this goes to Waxrat at Unix & Linux Stack Exchange.

Step 4: Delete .python_history

Delete the .python_history file in your homedir.

Step 5: Check if successful

Load up python3, enter some commands, exit, and check if the .python_history file exists. If it does not, you have been successful.

Update (2025)

With Python 3.13, it appears that this method no longer works. However, chmod 000 .python_history does appear to work now.

Update (2026)

With Python 3.14, it appears that this method throws an error each line. However, the following line in .bash_aliases will prevent creation of a Python interpreter history file while retaining scrollback:

export PYTHON_HISTORY=/dev/null