Я пытался выяснить, как сохранить и прочитать историю моих команд python в консоли ptpython
, но не смог этого сделать. До сих пор все мои усилия были вариациями этого ответа . Тем не менее, я до сих пор не могу читать в моей истории.
Я просто хотел бы иметь возможность нажимать стрелки «вверх» и «вниз» до go с помощью моих команд python из предыдущего сеанса консоли ( не текущей консоли сеанс, в котором я нахожусь). Вот что у меня есть в моем $PYTHONSTARTUP
файле:
# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pystartup, and set an environment variable to point
# to it: "export PYTHONSTARTUP=/home/user/.pystartup" in bash.
#
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
# full path to your home directory.
import atexit
import os
import readline
import rlcompleter
import sys
try:
from ptpython.repl import embed
except ImportError:
print('ptpython is not available: falling back to standard prompt')
else:
sys.exit(embed(globals(), locals()))
historyPath = os.path.expanduser("~/.ptpython/history")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
atexit.register(save_history)
readline.parse_and_bind('tab: complete')
del os, atexit, readline, rlcompleter, save_history, historyPath
И моя переменная $PYTHONSTARTUP
:
$ echo $PYTHONSTARTUP
/Users/[redacted]/.pystartup
Я на Python 3.7.3, macOS 10.14.6 и pt python 2.0.4.
Спасибо