2
votes

I installed Python 3.5.1 on my Mac, alongside 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. However, whenever I try to run a command or file with python3 in Terminal, it gives me this error:

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

and also pops up with a message saying "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.

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.

3
If I had to guess it looks as if your py3 and py2 installations are conflicting. Line 123 appears to me to be formatted per python3 syntax and that's why your py2 interpreter throws a syntax error. - Brian Cain
How did you install python3? - Mike Lane
@MikeLane from the python website. Just downloaded the installer and ran it. - Sasha Ahrestani
@SashaAhrestani so how did you solve it? - The Wanderer
Check what environment variables you have set. Make sure there aren't any related to Python. - Iguananaut

3 Answers

2
votes

Most probably $PYTHONPATH is set to use your Python2.x. To verify this type

echo $PYTHONPATH

If you see path related to Python2.x, try unsetting it.

unset PYTHONPATH
1
votes

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. But the errors you are getting are due to syntax incompatibility between your scripts and the python interpreter you are using. Study the changes in python3.x version and make changes in your scripts accordingly. You can have a look at this for your reference.

-1
votes

There is conflict because you set in environment twice versions of python and you have certainly set PYTHONPATH variable to python 2.7

So you can reset env path in script to get only as you want before run python

I don't know procedure in Mac OS by in Windows Env I do that script

for exemple in ps1 script

$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

In your initial case, you run python 3 and add Lib for python2. So it can work because py3 use unicode base encoding and py2 use ANSI encoding