Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable python history saving?

Tags:

python-3.x

I am on linux (OpenSuse) machine using CPython with

22;~/.../XO/> python3 --version
Python 3.6.10

I would like to setup saving/loading python interactive shell history separately for each working directory in which a session has been started. I am trying to do it in /etc/pythonstart having export PYTHONSTARTUP=/etc/pythonstart

The relevant content of my /etc/pythonstart is

import os
import readline
import atexit

historyFile = ".pyhistory"
cwd         = os.getcwd()

historyPath = cwd + "/" + historyFile

def save_history():
    try:
        readline.write_history_file(historyPath)
    except:
        pass

# read history
if os.path.exists(historyPath):
    readline.set_history_length(100)
    readline.read_history_file(historyPath)

# save history
atexit.register(save_history)

It almost works, except python also saves and reads history in ~/.python_history and it gets merged for all sessions. Is there a way to disable the later behaviour and if yes, how?

like image 630
R. Matveev Avatar asked Jun 27 '26 00:06

R. Matveev


1 Answers

I have the below in my zshrc:

export PYTHONSTARTUP=~/.config/python/pythonrc

And in the pythonrc file:

import readline
readline.write_history_file = lambda *args: None

It directly suppresses writing the history file instead of disabling history entirely. Source

(I'm using python 3.8.5)

like image 65
Saurabh Avatar answered Jun 28 '26 19:06

Saurabh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!