简体   繁体   English

安装了Python 3,运行时出错

[英]Python 3 installed, error when running

I installed Python 3.5.1 on my Mac, alongside Python 2.7.10 .我在 Mac 上安装了Python 3.5.1和 Python 2.7.10 When I run python --version , it returns Python 2.7.10 , and when I run python3 --version , it returns Python 3.5.1 , which should confirm that Python 3 correctly installed.当我运行python --version ,它返回Python 2.7.10 ,当我运行python3 --version ,它返回Python 3.5.1 ,这应该确认 Python 3 已正确安装。 However, whenever I try to run a command or file with python3 in Terminal , it gives me this error:但是,每当我尝试在Terminal 中使用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致命的Python错误:Py_Initialize:无法加载文件系统的编解码器文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/初始化的.py”,线路123提高CodecRegistryError,\\ ^语法错误:无效语法

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当前线程 0x00007fff72bb4000(最近一次调用):中止陷阱:6 Farshids-MacBook-Pro-2:mysite Sasha$ python3 致命 Python 错误:Py_Initialize:无法加载文件系统编解码器 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当前线程 0x00007fff72bb4000(最近调用优先):中止陷阱:6

and also pops up with a message saying "Python quit unexpectedly."并且还会弹出一条消息"Python quit unexpectedly." As you can see in the error, it mentions 2.7, which means that it was probably trying to run the code using 2.7.正如您在错误中看到的,它提到了 2.7,这意味着它可能试图使用 2.7 运行代码。

How do I fix this?我该如何解决? By the way, I installed Python 3 to follow a Django tutorial that does not work without Python 3, and I'm so knowledgeable about Python to be able to adjust.顺便说一句,我安装了 Python 3 以遵循 Django 教程,该教程在没有 Python 3 的情况下无法运行,而且我对 Python 非常了解,因此能够进行调整。

Most probably $PYTHONPATH is set to use your Python2.x.很可能 $PYTHONPATH 设置为使用您的 Python2.x。 To verify this type验证此类型

echo $PYTHONPATH

If you see path related to Python2.x, try unsetting it.如果您看到与 Python2.x 相关的路径,请尝试取消设置。

unset PYTHONPATH

You can keep the shebang line (starting with #! on the first line) as python3 (#!/usr/bin/python3.5) path if you are executing them as executables.如果将它们作为可执行文件执行,则可以将 shebang 行(第一行以 #! 开头)保留为 python3 (#!/usr/bin/python3.5) 路径。 But the errors you are getting are due to syntax incompatibility between your scripts and the python interpreter you are using.但是你得到的错误是由于你的脚本和你正在使用的 python 解释器之间的语法不兼容。 Study the changes in python3.x version and make changes in your scripts accordingly.研究 python3.x 版本中的更改并相应地对脚本进行更改。 You can have a look at this for your reference.你可以看看这个供你参考。

There is conflict because you set in environment twice versions of python and you have certainly set PYTHONPATH variable to python 2.7存在冲突,因为您在环境中设置了两次 python 版本,并且您肯定已将 PYTHONPATH 变量设置为 python 2.7

So you can reset env path in script to get only as you want before run python因此,您可以在运行 python 之前重置脚本中的 env 路径以仅根据需要获取

I don't know procedure in Mac OS by in Windows Env I do that script我不知道 Mac OS 中的程序,在 Windows Env 中我执行该脚本

for exemple in ps1 script例如在 ps1 脚本中

$Env:Py2 = "C:\Python27"

$Env:PYTHONPATH = "$Env:Py2;$Env:Py2\Lib;$Env:Py2\DLLs;$Env:Py2\Lib\lib-tk"
$Env:Path += ";$Env:Py2;$Env:Py2\Scripts\"
python

That run python 2.7运行python 2.7

In your initial case, you run python 3 and add Lib for python2.在您的初始情况下,您运行 python 3 并为 python2 添加 Lib。 So it can work because py3 use unicode base encoding and py2 use ANSI encoding所以它可以工作,因为 py3 使用 unicode 基本编码而 py2 使用 ANSI 编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行新安装的`meld`的Python语法错误 - Python syntax error running freshly installed `meld` 错误说正在运行的 Python 是 3.4,但安装了 3.5 - Error says the running Python is 3.4, but 3.5 is installed 运行已安装的脚本时出现Python ImportError - Python ImportError when running an installed script Django已安装,但在运行时在虚拟环境中引发导入错误:sudo python manage.py collectstatic - Django installed but throws import error in virtual env when running: sudo python manage.py collectstatic 在CMD中运行python时丢失了numpy(与anaconda一起安装的python) - numpy missing when running python in CMD (python installed with anaconda) 尝试运行Rodeo(“运行python时出错,'未安装Jupyter”) - Trying to run Rodeo ('there was an error running python', 'Jupyter is not installed') 从cronjob运行时,Python找不到自定义安装的模块 - Python does not find custom installed modules when running from a cronjob 定义导入和Python项目结构以允许在本地和安装时运行 - Define imports and Python project structure to allow running locally and when installed 运行ipython时,python没有看到已安装的模块 - Python doesn't see installed module when running ipython 运行 python 可执行文件时没有名为“请求”的模块,即使它已安装 - No module named 'requests' when running python executable, even it's installed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM