安装了Python 3,运行时出错

标签 python macos python-2.7 python-3.x

我在 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/

相关文章:

swift - macOS 上的 AVAudioInputNode 仅输出静音

python - 如何获取模块中的所有变量,但不包括在该模块中导入的变量

python - 将 Flask 应用程序部署到 Bluehost(或任何)网络服务器是如何工作的?

postgresql - 如何在维护数据的同时升级 docker 容器中的 postgres? 10.3 到最新的 10.x 或 12.x

c++ - g++ 正则表达式头文件

python - SQLAlchemy 中使用 keys() 的列名顺序

python - Pycharm 中的 traitlets.traitlets.TraitError

python - 将用户导入 Firebase 时密码错误(使用 SHA256 的 password_hash)

python - 如何正确地将 JSON 转换为 Python 对象?

python - 处理python中的telnetlib超时错误