Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Should I Set Default Python Version In Windows?

I installed Python 2.6 and Python 3.1 on Windows 7 and set environment variable: path = d:\python2.6.

When I run python in cmd, it displays the python version 2.6, which is what I want!
But, when I wrote a script in a bat file and ran it, the displayed python version was 3.1.

import sys print (sys.version) 

What's going on here?

like image 757
rooney Avatar asked Feb 23 '11 06:02

rooney


People also ask

How do I change default Python version in Windows?

As a standard, it is recommended to use the python3 command or python3. 7 to select a specific version. The py.exe launcher will automatically select the most recent version of Python you've installed. You can also use commands like py -3.7 to select a particular version, or py --list to see which versions can be used.

Which Python version is best for Windows?

If it's for something like self-study, then you can use the latest stable minor releases of either of them, like— Python 2.7. 15 or Python 3.7. 2. There are plenty of differences between Python 2.

How do I make Python 3 My default Windows?

Set your preferred default version by setting the PY_PYTHON environment variable (e.g. PY_PYTHON=3.7) . You can see what version of python is your default by typing py . You can also set PY_PYTHON3 or PY_PYTHON2 to specify default python 3 and python 2 versions (if you have multiple).


2 Answers

This is if you have both the versions installed.

Go to This PCRight-clickClick on PropertiesAdvanced System Settings.

You will see the System Properties. From here navigate to the Advanced Tab -> Click on Environment Variables.

You will see a top half for the user variables and the bottom half for System variables.

Check the System Variables and double-click on the Path (to edit the Path).

Check for the path of Python(which you wish to run i.e. Python 2.x or 3.x) and move it to the top of the Path list.

Restart the Command Prompt, and now when you check the version of Python, it should correctly display the required version.

like image 132
Aditya Deshpande Avatar answered Oct 09 '22 05:10

Aditya Deshpande


The Python installer installs Python Launcher for Windows. This program (py.exe) is associated with the Python file extensions and looks for a "shebang" comment to specify the python version to run. This allows many versions of Python to co-exist and allows Python scripts to explicitly specify which version to use, if desired. If it is not specified, the default is to use the latest Python version for the current architecture (x86 or x64). This default can be customized through a py.ini file or PY_PYTHON environment variable. See the docs for more details.

Newer versions of Python update the launcher. The latest version has a py -0 option to list the installed Pythons and indicate the current default.

Here's how to check if the launcher is registered correctly from the console:

C:\>assoc .py .py=Python.File  C:\>ftype Python.File Python.File="C:\Windows\py.exe" "%1" %* 

Above, .py files are associated with the Python.File type. The command line for Python.File is the Python Launcher, which is installed in the Windows directory since it is always in the PATH.

For the association to work, run scripts from the command line with script.py, not "python script.py", otherwise python will be run instead of py. If fact it's best to remove Python directories from the PATH, so "python" won't run anything and enforce using py.

py.exe can also be run with switches to force a Python version:

py -3 script.py       # select latest Python 3.X version to be used. py -3.6 script.py     # select version 3.6 specifically. py -3.9-32 script.py  # select version 3.9 32-bit specifically. py -0                 # list installed Python versions (latest PyLauncher). 

Additionally, add .py;.pyw;.pyc;.pyo to the PATHEXT environment variable and then the command line can just be script with no extension.

like image 42
Mark Tolonen Avatar answered Oct 09 '22 06:10

Mark Tolonen