我在 Mac 上安装了 Python 3.5.1,以及 Python 2.7.10。当我运行 python --version
时,它返回 Python 2.7.10
,当我运行 python3 --version
时,它返回 Python 3.5.1
,这应该确认 Python 3 已正确安装。但是,每当我尝试在终端中使用python3运行命令或文件时,都会出现以下错误:
Fatal Python error: Py_Initialize: unable to load the file system codec File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/init.py", line 123 raise CodecRegistryError,\ ^ SyntaxError: invalid syntax
Current thread 0x00007fff72bb4000 (most recent call first): Abort trap: 6 Farshids-MacBook-Pro-2:mysite Sasha$ python3 Fatal Python error: Py_Initialize: unable to load the file system codec File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/init.py", line 123 raise CodecRegistryError,\ ^ SyntaxError: invalid syntax
Current thread 0x00007fff72bb4000 (most recent call first): Abort trap: 6
并且还会弹出一条消息:“Python 意外退出。”
正如您在错误中看到的,它提到了 2.7,这意味着它可能正在尝试使用 2.7 运行代码。
我该如何解决这个问题?顺便说一句,我安装了 Python 3 来遵循 Django 教程,如果没有 Python 3,该教程就无法工作,而且我对 Python 非常了解,能够进行调整。
最佳答案
很可能 $PYTHONPATH 设置为使用您的 Python2.x。验证此类型
echo $PYTHONPATH
如果您看到与 Python2.x 相关的路径,请尝试取消设置。
unset PYTHONPATH
关于安装了Python 3,运行时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35978794/