macos - OS X - 谁能解释为什么我在安装 ipython3 后在我的 .pythonrc.py 文件中收到操作系统错误,或者给我一个比这更强大的解决方案?

标签 macos python-3.x ipython

我使用 pip3 安装了 ipython3。

pip3 install ipython[all]

当我运行 python 时,它遇到了来 self 的 ~/.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

谁能告诉我是否有更可靠的解决方案或者为什么会发生这种情况?

最佳答案

我刚找到答案:

import gnureadline as readline

基于链接:

gnureadline for IPython OSX

On OSX, if you are using the built-in Python shipped by Apple, you will be missing a proper readline implementation as Apple ships instead a library called libedit that provides only some of readline’s functionality. While you may find libedit sufficient, we have occasional reports of bugs with it and several developers who use OS X as their main environment consider libedit unacceptable for productive, regular use with IPython.

Therefore, IPython on OS X depends on the gnureadline module. We will not consider completion/history problems to be bugs for IPython if you are using libedit.

关于macos - OS X - 谁能解释为什么我在安装 ipython3 后在我的 .pythonrc.py 文件中收到操作系统错误,或者给我一个比这更强大的解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29572484/

相关文章:

java - 由于 OSX 中的其他线程,Platform.exit() 未结束 Javafx

ios - 在 Swift REPL 中使用 iphonesimulator sdk 和 macosx sdk 有什么区别?

macos - rsync 备份到外部硬盘 exFat 失败

python-3.x - 如何根据特定条件将普通数据框转换为多索引

python - 如何配置 IPython 在每次启动时发出相同的 "magic"命令?

macos - 在 Mac 上的 Chrome 中永久启用 CORS

python-3.x - 代码不会要求用户输入?

python-3.x - 如何通过套接字发送消息序列?

ipython - 如何在没有输出的情况下打开 IPython 笔记本?

python - 如何在python输出控制台中仅清除最后一行?