简体   繁体   中英

How to set PYTHONPATH in Windows?

I tried to run a .py file by typing python twitterstream.py > output.txt in the command prompt of windows 10. It then said

fatal python error: Py_Initialize: unable to load the file system codec
File "C:\Python27\Lib\encodings\__init__.py", line 123 raise CodecRegistryError,\
SyntaxError: invalid syntax

From what I saw through a google search, the solution may be to change the PYTHONPATH env variable. However, I had PYTHONPATH set to C:\\Python27 for both the user and system variables, but I still get the same error in the command prompt

I also read http://bugs.python.org/issue11288 , and tried to change all PYTHONPATH system variable which originally pointed to C:\\Python27 and changed it to C:\\Python34 but that didn't solve the problem

I then followed https://docs.python.org/3/using/windows.html#python-launcher-for-windows , but when I type py it says Python 2.7.9 .. , but for py -3 I get a popup saying python.exe has stopped working and

 fatal python error: Py_Initialize: unable to load the file system codec
File "C:\Python27\Lib\encodings\__init__.py", line 123 raise CodecRegistryError,\
SyntaxError: invalid syntax

also, py hello.py says from python 2.7.9 ... , but changing #! python #! python to #! python3 #! python3 gives the same error I said before

Even if I set PATH AND PYTHONPATH user variables to C:\\Python34;C:\\Python27;C:\\Python27\\Scripts;C:\\Python34\\Scripts , along with the PythonPath system variable to C:\\Python34;C:\\Python27;C:\\Python27\\Scripts;C:\\Python34\\Scripts , but the error was the same as before

For some reason by typing only the "py" file name in the command prompt it worked for me. So I typed myScript.py and it ran correctly. Before this I typed "python myScript.py" and it produced the same error that you experienced. I have the environment variables set PYTHONPATH=c:\\python27 and PYTHONHOME=c:\\python27 . I hope this helps.

At the start of your program include the line

#!/usr/bin/python3

Windows will use this to run your code in Python 3.4 rather than 2.7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM