我使用 pip3 安装了 ipython3。
pip3 install ipython[all]
当我运行 python 时,我的 ~/.pythonrc.py 文件遇到错误(仅在 Python 3 上,不在 Python 2 上)。
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
File "/Users/Mike/.pythonrc.py", line 43, in <module>
readline.read_history_file(history_path)
OSError: [Errno 22] Invalid argument
我在 stackoverflow 上找不到答案,但进行了一些挖掘并找到了解决方案。我不确定为什么现在这样有效,或者是否有更好的解决方案。
在 .pythonrc.py 文件中:
try:
import readline
except ImportError:
pass
history_path = os.path.expanduser('~/.pyhistory')
if os.path.isfile(history_path):
readline.read_history_file(history_path)
atexit.register(lambda x=history_path: readline.write_history_file(x))
解决方案是导入 gnureadline :
try:
import gnureadline as readline
except ImportError:
pass
谁能告诉我是否有更强大的解决方案或者为什么会发生这种情况?