简体   繁体   中英

python running python2 instead of python3 on windows

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 )

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.

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