Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.6 is not default when I run a script

I have set the environment variables so that python 3.6 is the version that is run when I use the python command in the terminal, but when I run a python script from the terminal it uses python 2.7.

I have included a folder called MyScripts in the path so that i can run these scripts directly from the terminal without specifying the path.

Running just python shows Python 3.6.4

> python

But executing the script shows 2.7.14. This is the script v27.py

import sys
print(sys.version)
print(sys.executable)

And this is the output

output from v27.py

like image 239
Asle Berge Avatar asked Sep 28 '26 10:09

Asle Berge


1 Answers

As you are running the Python Script (as a executable) without specifying python in command prompt, it will not use the python path set in Environment Variables. Instead, windows will run the Default Program associated for that extension. You can find the associated program under Control Panel\All Control Panel Items\Default Programs\Set Associations

Either you can go to ControlPanel to change the Default Program or your can run below command in cmd (need elevated permission to do this)

C:\> assoc .py=Python
C:\> ftype Python="C:\Users\asleb\AppData\Local\Programs\Python\Python36-32\python.exe" "%1" %*

Also, if you want to run without extension (like v27 instead of v27.py) then, you need to add .py to the PATHEXT environment variable.

like image 88
dhilmathy Avatar answered Sep 30 '26 03:09

dhilmathy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!