.pythonrc.py不能在macOS上使用Python 2.7
这是我在运行Debian测试的笔记本电脑上设置的.pythonrc.py不能在macOS上使用Python 2.7,python,bash,Python,Bash,这是我在运行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:
.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
文件,附件如下:
HISTCONTROL=ignoreboth
HISTSIZE=2000
HISTFILESIZE=4000
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
bind '"C-\e[A": reverse-search-history'
bind '"C-\e[B": forward-search-history'
alias pythonic='python -i -c "from user import *"'
PYTHONSTARTUP=$HOME/.pythonrc.py
自动完成和历史搜索在Bash上正常工作,但是当通过pythonic
别名运行Python时,相同的功能似乎不能像预期的那样工作。如果可能的话,有人能给我解释一下这种行为的原因以及如何解决它吗?在中,readline
moude使用libedit
而不是GNU readline
,后者具有不同的配置语法
您可以安装gnureadline
,或者使用自制程序安装使用GNU readline
的新python二进制文件,或者更改rc配置
正如您指定的那样,-c'import user'
,user
模块看起来并执行.pythonrc.py
,其功能与PYTHONSTARTUP
相同,因此您只需要一个
此外,~/.profile
中定义的shell变量在bash进程本身中是可行的,但是python作为bash的子进程运行,您必须导出PYTHONSTARTUP
以使其可供python使用:
export PYTHONSTARTUP=$HOME/.pythonrc.py
在中,readline
moude使用libedit
而不是具有不同配置语法的GNU readline
您可以安装gnureadline
,或者使用自制程序安装使用GNU readline
的新python二进制文件,或者更改rc配置
正如您指定的那样,-c'import user'
,user
模块看起来并执行.pythonrc.py
,其功能与PYTHONSTARTUP
相同,因此您只需要一个
此外,~/.profile
中定义的shell变量在bash进程本身中是可行的,但是python作为bash的子进程运行,您必须导出PYTHONSTARTUP
以使其可供python使用:
export PYTHONSTARTUP=$HOME/.pythonrc.py
我建议将您的键绑定添加到
.inputrc
,任何使用Readline库的应用程序都可以使用该键绑定。然后您不需要将它们添加到.profile
或.pythonrc.py
格式不同(并且稍微简单一些):
(Readline库本身不涉及读取或写入历史文件,因此该部分需要保留在Python rc文件中。)我建议将您的键绑定添加到
.inputrc
,任何使用Readline库的应用程序都会使用它。然后您不需要将它们添加到.profile
或.pythonrc.py
格式不同(并且稍微简单一些):
(Readline库本身不涉及读取或写入历史文件,因此该部分需要保留在Python rc文件中。)感谢您的建议,但也导出了
PYTHONSTARTUP
环境变量,pythonic
保持不工作。请尝试env|grep'PYTHONSTARTUP'
检查变量是否存在,如果不存在,请仔细检查您的配置文件是否执行。grep
返回变量,因此它已被定义。export-p | grep PYTHON
如何?@rudicangioti更新,在MacOS中,python使用libedit而不是GNU readline。感谢您的建议,但同时也导出了PYTHONSTARTUP
环境变量,pythonic
一直不工作。请尝试env | grep'PYTHONSTARTUP'
检查var是否存在,如果不存在,仔细检查您的.profile
是否得到执行。grep
返回变量,因此它被定义。那么export-p | grep PYTHON
呢?@rudicangioti更新,在MacOS中,python使用libedit而不是GNU readline。python控制台的行为保持不变。python控制台的行为保持不变。