.Python.pythonrc.py不在m上使用python2.7

2024-05-13 10:01:42 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我在运行Debian测试的笔记本电脑上设置的.pythonrc.py文件的内容:

import os, readline, rlcompleter, atexit
history_file= os.path.join(os.environ['HOME'], '.python_history')
try:
  readline.read_history_file(history_file)
except IOError:
  pass
readline.parse_and_bind("tab: complete")
readline.parse_and_bind('"\e[A": history-search-backward')
readline.parse_and_bind('"\e[B": history-search-forward')
readline.parse_and_bind('"C-\e[A": reverse-search-history')
readline.parse_and_bind('"C-\e[B": forward-search-history')

readline.set_history_length(8000)
atexit.register(readline.write_history_file, history_file)

del os, readline, rlcompleter, atexit, history_file, __file__

我在运行macOs Sierra的Macbook中有一份相同文件的副本,与.profile文件相结合,如下所示:

^{pr2}$

自动完成和历史搜索在Bash上可以正常工作,但是在通过pythonic别名运行Python时,相同的功能似乎无法正常工作。如果可能的话,有人能解释一下这种行为的原因以及如何解决它吗?在


Tags: and文件searchreadlineparseosbinddebian
2条回答

我建议将keybindings添加到.inputrc,任何使用Readline库的应用程序都会使用它。那么您不需要将它们添加到.profile或{}。在

格式不同(稍微简单一点):

"\e[A": history-search-backward
"\e[B": history-search-forward
"C-\e[A": reverse-search-history
"C-\e[B": forward-search-history

set history-size 8000

(Readline库本身并不关心对历史文件的读写,因此该部分需要保留在pythonrc文件中。)

MacOSreadlinemoudle使用libedit而不是{},这有不同的配置语法。在

您可以安装gnureadlinepackage,或者使用自制程序安装一个使用GNU readline的新python二进制文件,或者更改rc配置。在

正如您指定的-c 'import user'user模块查找并执行.pythonrc.py,其功能与PYTHONSTARTUP相同,因此您只需要一个。在

此外,在~/.profile中定义的shell变量在bash进程本身中是可行的,但是python作为bash的子进程运行,您必须导出PYTHONSTARTUP以使其对python可用:

export PYTHONSTARTUP=$HOME/.pythonrc.py

相关问题 更多 >