You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The New REPL Does Not Load My Command History #120766
This is something I've been using for years. With this startup file, I can enter the REPL and immediately up-arrow to scroll through the commands I used the last time I used the REPL.
With the new REPL there are two related things going wrong:
my command history is not loaded (when I immediately up-arrow, it does nothing)
my command history is not saved in my .pyhistory file (the file is touched but not otherwise modified)
Hummm, it looks like this code it's using readline to handle the story but the new REPL doesn't use readline at all so this will never work. Notice that this will also not work the same if you compile Python against libedit instead of readline because it has different history handling (to the point I know).
@ambv what do you think? Maybe we can monkey patch readline.read_history_file somehow in the new REPL but I am concerned of the side effects.
@whitequark, AFAIK that's a different issue.
The new REPL noes not use the readline Python module at all. readline.read_history_file won't affect it, regardless of the backend.
I'm seeing a possibly related issue but without a startup file. The first time Python 3.13 runs the history is there, but after exiting and running again the history has gone. Running an older Python populates the history with new entries but they're lost the next time Python 3.13 exits.
Git bisect tracked the issue to 38cfa92. The actual issue is in site.py, where it tries to import CAN_USE_PYREPL from __main__, which it's no longer located in as of that commit.
# _pyrepl.__main__ is executed as the __main__ module
from__main__importCAN_USE_PYREPL
exceptImportError:
CAN_USE_PYREPL=False
Changing this to from _pyrepl.main import CAN_USE_PYREPL or adding from .main import CAN_USE_PYREPL to the top of _pyrepl.__main__.py seems to fix it. (I couldn't make it work reliably at first but I think that was me forgetting that site.py is frozen)
@ambv, perhaps #130938 could be closed in favor of this issue? Underlying reason is same: the stdlib's readline module shadows history-related functions from the _pyrepl.readline.
I don't use the REPL as much as I used to, and these days don't really use versions before 3.13 all that often, so I'm only stumbling upon this issue now. I have a completions.py file in my interactive startup sequence which does effectively what @ericsnowcurrently's .pystartup file does. I managed to work around it by attempting a _pyrepl import and falling back to my startup if that fails:
try:
import _pyrepl
print("using _pyrepl")
except ImportError:
# ... do the old stuff here ...
print("using historical readline")
That seems to work. Is there some expectation that the switch when going from <= 3.12 to >= 3.13 should be completely transparent?
Built-in support for the $PYTHON_HISTORY env var was only added in 3.13. The setup people had before in their $PYTHONSTARTUP wasn't standardized and the on-disk format differs between GNU readline and libedit (aka editline). Additionally, the current history format of PyREPL is yet different. We do attempt to import old history files when encountering them, but once saved back, they might not be readable back in older Python versions (or $PYTHON_BASIC_REPL for that matter, which is admittedly a bit of a bummer).
Activity
pablogsal commentedon Jun 22, 2024
Hummm, it looks like this code it's using
readlineto handle the story but the new REPL doesn't usereadlineat all so this will never work. Notice that this will also not work the same if you compile Python againstlibeditinstead ofreadlinebecause it has different history handling (to the point I know).@ambv what do you think? Maybe we can monkey patch
readline.read_history_filesomehow in the new REPL but I am concerned of the side effects.whitequark commentedon Jun 29, 2024
I have made a workaround for this issue: #121160 (comment).
encukou commentedon Jul 1, 2024
@whitequark, AFAIK that's a different issue.
The new REPL noes not use the
readlinePython module at all.readline.read_history_filewon't affect it, regardless of the backend.domdfcoding commentedon Jul 1, 2024
I'm seeing a possibly related issue but without a startup file. The first time Python 3.13 runs the history is there, but after exiting and running again the history has gone. Running an older Python populates the history with new entries but they're lost the next time Python 3.13 exits.
Git bisect tracked the issue to 38cfa92. The actual issue is in
site.py, where it tries to importCAN_USE_PYREPLfrom__main__, which it's no longer located in as of that commit.cpython/Lib/site.py
Lines 527 to 532 in c7991cc
Changing this to
from _pyrepl.main import CAN_USE_PYREPLor addingfrom .main import CAN_USE_PYREPLto the top of_pyrepl.__main__.pyseems to fix it. (I couldn't make it work reliably at first but I think that was me forgetting thatsite.pyis frozen)hroncok commentedon Jul 1, 2024
@domdfcoding As this does not require a startup file, I belive it's a different (and more severe issue). I opened #121245
skirpichev commentedon Apr 11, 2025
@ambv, perhaps #130938 could be closed in favor of this issue? Underlying reason is same: the stdlib's readline module shadows history-related functions from the
_pyrepl.readline.smontanaro commentedon May 21, 2025
I don't use the REPL as much as I used to, and these days don't really use versions before 3.13 all that often, so I'm only stumbling upon this issue now. I have a
completions.pyfile in my interactive startup sequence which does effectively what @ericsnowcurrently's.pystartupfile does. I managed to work around it by attempting a_pyreplimport and falling back to my startup if that fails:That seems to work. Is there some expectation that the switch when going from <= 3.12 to >= 3.13 should be completely transparent?
ambv commentedon Jun 2, 2025
Built-in support for the
$PYTHON_HISTORYenv var was only added in 3.13. The setup people had before in their$PYTHONSTARTUPwasn't standardized and the on-disk format differs between GNU readline and libedit (aka editline). Additionally, the current history format of PyREPL is yet different. We do attempt to import old history files when encountering them, but once saved back, they might not be readable back in older Python versions (or$PYTHON_BASIC_REPLfor that matter, which is admittedly a bit of a bummer).