JLindros
JLindros

Reputation: 61

Windows 10 won't launch my python script with the correct version of python despite correct file association and paths.

In Windows 10, I have both Python3.5 and Python2.7 installed, and need to keep both. My script is written for Python3.5. If I launch the script with "python" or "py" prefix, it launches without any problems. However, when I invoke it directly, it fails with an error similar to python version conflict:

File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site.py", line 176 file=sys.stderr) ^ SyntaxError: invalid syntax

In my command prompt, I type

>python script.py

or

>py script.py

It works, but if I just run the script, either with ./script.py or just script.py (since the script is in my path) it fails with the conflict mentioned above.

My PATH, PYTHONHOME and PYTHONPATH all point to 3.5

>echo %PATH%    
C:\Users\user\AppData\Local\Programs\Python\Python35;C:\Users\user\AppData\Local\Programs\Python\Python35\Scripts;

>echo %PYTHONPATH%
C:\Users\user\AppData\Local\Programs\Python\Python35\lib

>echo %PYTHONHOME%
C:\Users\user\AppData\Local\Programs\Python\Python35

My python file association is set to py launcher, which works correctly if I add py to the beginning of my path.

>assoc .py

.py=Python.File

>ftype Python.File

Python.File="C:\WINDOWS\py.exe" "%L" %*

My py and python both have correct versions for the ones called in my path:

>py --version

Python 3.5.3

>python --version

Python 3.5.3

which python points to the correct version too.

>which python

/cygdrive/c/Users/user/AppData/Local/Programs/Python/Python35/python

The only other thing I could think of was that cygwin or MSDOS shell have their own python interpreter they are using that interferes, but I can't find a version of python in either directory.

There must be some other file association overriding this one somewhere in Windows 10 that I don't know about. Unix shebang would solve this whole thing, but Windows is just annoying. Anyone have an clues?

Upvotes: 3

Views: 7146

Answers (2)

Jan Bodnar
Jan Bodnar

Reputation: 11657

There is also one tweak to the issue. If you run Python scripts without the .py extension (using the PATHEXT environment variable) , you need to update the HKEY_CLASSES_ROOT\py_auto_file\shell\open\command registry ke to "C:\PathToPython" "%1" %* The %* part might be missing, which ensures that arguments are correctly passed to Python.

Upvotes: 1

JLindros
JLindros

Reputation: 61

SOLVED! assoc py was incorrect. In the regedit I found another key that had been added by an IDE, which added a command to edit with it's own python27 version. It somehow was taking precedence over the real python. After deleting this key, and resetting the Windows associate extension .py, it worked correctly.

Thanks to suggestion from @Jean-FrançoisFabre.

Upvotes: 3

Related Questions