【问题标题】:Where is the Python interpreter startup script for putting a history function?用于放置历史函数的 Python 解释器启动脚本在哪里?
【发布时间】:2019-12-31 07:22:42
【问题描述】:

我想添加这样的功能:

def history():
  import readline
  for i in range(readline.get_current_history_length()):
    print (readline.get_history_item(i + 1))

这样每当我在 Python shell 中(例如运行 python3)或在 Python 代码中使用 ipdb 遇到断点时,我都可以调用 history()

我在哪里添加此代码? (MacOS)

【问题讨论】:

标签: python python-3.x


【解决方案1】:

您也许可以使用 PYTHONSTARTUP 环境变量来实现这一点。
创建一个包含此代码的脚本,并将变量导出到您的 shell 的配置文件中

https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP

【讨论】:

  • 我不确定是什么问题,但是当我运行cat $PYTHONSTARTUP 时,命令只是挂起,没有打印任何内容
  • @Michael 你的意思是 echo $PYTHONSTARTUP?
  • echo 不打印任何东西
  • 这意味着它是空的,用你想要的代码创建一个python文件,然后将export PYTHONSTARTUP='path_to_your_file'添加到你的shell配置中
猜你喜欢
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 2015-08-09
相关资源
最近更新 更多