2

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 to #! 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

25
  • 1
    It seems you have PYTHONHOME set to C:\Python27. Generally there's no reason to set either PYTHONPATH or PYTHONHOME, not with the official Python distributions. Unset these variables. There are better ways to manage sys.path for a particular installation that don't cause such conflicts. Commented Sep 27, 2015 at 23:24
  • i don't see PYTHONHOME as an environment variable. And when you say unset, you mean delete them? When I tried that on PYTHONPATH, I got the same error as before Commented Sep 28, 2015 at 0:14
  • I can reproduce the error if I first enter set "PYTHONHOME=C:\Python27". This overrides Python's default sys.prefix, by which it finds its standard library. Python 3 obviously can't use Python 2's standard library. In the command prompt enter set PYTHON. Does this list PYTHONHOME or just PYTHONPATH? If it's just PYTHONPATH, that should not cause that error when running py -3. Something else is wrong. Commented Sep 28, 2015 at 0:39
  • when I enter set PYTHON it says Environment variable PYTHON not defined Commented Sep 28, 2015 at 0:40
  • That means neither PYTHONHOME nor PYTHONPATH is set. In that command prompt do you get the error for py -3? Commented Sep 28, 2015 at 0:42

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

On Windows, py is the Python Launcher. This is specific tool for Windows made and shipped with Windows installers to find and use different versions of Python. Linux uses shebangs to tell the OS what program to use to run a file whereas Windows uses file extensions (this is why in Linux you don't always need a file extension). The launcher helps overcome portability issues when developing code for Linux and Windows (more info is in PEP397). Typing just python for you executes C:\python27\python.exe because you added it to your path.
0

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

8 Comments

but even if I try that on twitterstream.py, I get the same error. I also got that error for hello.py as explained in that python documentation page I linked to
don't run it as python twitterstream.py, just run twitterstream.py from the command prompt and it should launch python3 instead of the default 2.7
ok with that, I don't get the error message anymore. However, I don't think it worked properly because there should be some content in output.txt, but it is still blank. The code for twitterstream.py is github.com/uwescience/datasci_course_materials/blob/master/…
do you get the correct output without piping output to your text file? Windows tends to leave the output file empty until the process finishes, so if it takes a long time it can look like its failed (esp if running from task schedular)
when I run twitterstream.py in Ninja, in the output I see some HTML content, which I assume is the correct output. However, output.txt is still empty
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.