1

Well I have this code:

#!/usr/bin/env python3

for i in range(15):
    print(i, end=' ')

when I run it like this in command prompt:

prog1.py

I get:

C:\Users\Ja\Desktop>prog1.py
File "C:\Users\Ja\Desktop\prog1.py", line 4
print(i, end=' ')
            ^
SyntaxError: invalid syntax

but when I run it like this:

py prog1.py

it works fine. It's running python2.7.15 instead of python3.6.5 (I tested it using sys.version). ( It's the same for icon clicks )

4
  • This is very difficult to assess without knowing more about your machine. Can you post your PythonPath environment variable and the result of sys.path from inside a Python script? Commented Aug 29, 2018 at 10:45
  • sys.path: ['C:\\Users\\Ja\\Desktop', 'C:\\Users\\Ja\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip', 'C:\\Users\\Ja\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs', 'C:\\Users\\Ja\\AppData\\Local\\Programs\\Python\\Python36-32\\lib', 'C:\\Users\\Ja\\AppData\\Local\\Programs\\Python\\Python36-32', 'C:\\Users\\Ja\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages'] Commented Aug 29, 2018 at 10:54
  • This is my path: C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\WINDOWS\System32\OpenSSH\;C:\Users\Ja\AppData\Local\Programs\Python\Python36-32\Scripts\;C:\Users\Ja\AppData\Local\Programs\Python\Python36-32\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Users\Ja\AppData\Local\Microsoft\WindowsApps;C:\Program Files (x86)\CodeBlocks\MinGW\bin; Commented Aug 29, 2018 at 10:55
  • 1
    Add path in your path variable where python.exe file is located. Like mine is located in C:\Anaconda3. Commented Aug 29, 2018 at 10:59

1 Answer 1

1

You may be under the impression that Windows understands the "shebang line":

#!/usr/bin/env python3

It does not. All Windows knows is that .py files can be run by Python. So you need to update your file association, or run the correct version of Python explicitly, passing the path to your script as an argument.

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

1 Comment

@Igracx33: Rather than "link and run" would you like to tell us something specific?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.