146

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?

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
rooney
  • 2,233
  • 4
  • 19
  • 10
  • Python code in a .bat file won't be interpreted by Python, instead cmd.exe tries to interpret it which won't work. Put the Python code in a .py file and then in the .bat put `python scriptfile.py`. – martineau Feb 23 '11 at 08:42
  • thank you anyway!I find the right way,I modify the value of the registry item in HKEY_LOCAL_CLASS\Applications\Python.exe\shell\open\command,and then it works right for bat file – rooney Feb 23 '11 at 09:57

19 Answers19

155

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.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Aditya Deshpande
  • 1,716
  • 1
  • 10
  • 12
  • 18
    Make sure to get the Scripts part of the path too: C:\Python27;C:\Python27\Scripts\; – eliteproxy May 01 '19 at 04:02
  • 9
    One detail to watch out for here: if you did not tick 'Install for all users' during custom installation (or did not install as an administrator), Python will get installed to %LOCALAPPDATA%\Programs\Python, and get added to the PATH in your _user_ environment variables. If the other Python did get installed as an admin/for all users, it will go in C:\Program Files\, and be added to PATH in the _system_ environment variables. Usually, user environment variables override system ones, but PATH is always (system PATH + user PATH). This is what's just tripped me up, so I thought I'd note it here. – Ryan Plant Mar 16 '20 at 14:22
  • 5
    Just don't add any of them to the path and use the Python launcher. Why edit the path every time you switch? You can also create virtual environments with the installed python of choice with "py -x.y -m venv " where x.y is the Python version (must already be installed). – Mark Tolonen Jan 10 '21 at 18:52
  • 4
    doesn't work anymore with python 3.11 (worked with 3.9) – JinSnow Nov 19 '22 at 17:20
  • Note that if you are installing using Windows and you select "Add to python path", the installer automatically puts the newest version first, if there are existing versions. – Ralph Willgoss Jun 23 '23 at 13:31
  • this is obsolete – Marcel Aug 04 '23 at 12:59
107

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.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • @TomW, next time I'll use my crystal ball. It is quite possible the installer is different for a version just released. This is a two-year old answer. Anymore, I'd use the `py.exe` launcher that comes with Python 3.3 to control the version of Python a script uses via a shebang comment. – Mark Tolonen May 18 '13 at 14:05
  • 1
    In hindsight the tone wasn't really appropriate so I apologise - I'd like to amend my comment, which I don't seem to be able to do - although the point of StackOverflow is to be helpful, so I think it's appropriate to point out that the behaviour doesn't seem to be the same anymore. – Tom W May 18 '13 at 17:00
  • Also, I think prior to Python 3.3, there wasn't an option to add Python to the PATH, but there is now. However, when Python registers itself with Windows, previously it would only associate one explicit `python.exe` with file extensions, so last installed with the registration option wins. Python 3.3 changes that with the Python Launcher and any Python version can be explicitly specified with a comment in the script. – Mark Tolonen May 19 '13 at 20:14
  • I had a Launcher.zip in the python installed folder (same level as the multiple python version). I just unziped the folder. Run cmd, type "py", and the launcher was recognized and send the proper python version. – JinSnow Feb 10 '17 at 11:36
78

Running 'py' command will tell you what version you have running. If you currently running 3.x and you need to switch to 2.x, you will need to use switch '-2'

py -2

If you need to switch from python 2.x to python 3.x you will have to use '-3' switch

py -3

If you would like to have Python 3.x as a default version, then you will need to create environment variable 'PY_PYTHON' and set it's value to 3.

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
  • 3
    this one is very useful, no need to edit registry keys...when running python 2 scripts in command line... one can do `py -2 test.py` ...then by default `python test.py` if it's python 3 based code – sasori Apr 15 '17 at 15:52
  • 1
    PY_PYTHON didn't work, removing python(undesirable version) path from PATH variable worked for me. – Naveen Kumar Jun 23 '20 at 14:53
  • `py -0` to list available versions. Then select from the list e.g. `py -3.10`, if you have `3.10` in your list. – pds Aug 04 '23 at 09:36
27

If you know about Environment variables and the system variable called path, consider that any version of any binary which comes sooner, will be used as default.

Look at the image below, I have 3 different python versions but python 3.8 will be used as default since it came sooner than the other two. (In case of mentioned image, sooner means higher!)

enter image description here

AmiNadimi
  • 5,129
  • 3
  • 39
  • 55
23

If you are a Windows user and you have a version of Python 3.3 or greater, you should have the Python Launcher for Windows installed on your machine, which is the recommended way to use for launching all python scripts (regardless of python version the script requires).

As a user

  • Always type py instead of python when running a script from the command line.

  • Setup your "Open with..." explorer default program association with C:\Windows\py.exe

  • Set the command line file extension association to use the Python Launcher for Windows (this will make typing py optional). In an Admin cmd terminal, run:

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

    ftype Python.NoConFile="C:\Windows\pyw.exe" "%L" %*

  • Set your preferred default version by setting the PY_PYTHON environment variable (e.g. PY_PYTHON=3.11). 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).

  • If you need to run a specific version of python, you can use py -M.m (where M is the major version and m is the minor version). For example, py -3 will run any installed version of python 3.

  • List the installed versions of python with py -0.

As a script writer

  • Include a shebang line at the top of your script that indicates the major version number of python required. If the script is not compatible with any other minor version, include the minor version number as well. For example:

    #!/usr/bin/env python3

    Note: (see this question) If python3 does not work for you, ensure that you've installed python from the Windows Store (e.g. via winget install --id 9NRWMJP3717K, as the winget package Python.Python.3.11 does not appear to include a python3.exe).

  • You can use the shebang line to indicate a virtual environment as well (see PEP 486 below).


See also

Casey Kuball
  • 7,717
  • 5
  • 38
  • 70
  • Great answer. For your 2nd point "Setup your Open with..." - I just want to add here that if you've already got Python 2.7 set as the default Open With, then you'll need to disassociate that first before you can set Python 3.x as your default Open With. For example, C:\Python27\python.exe needs to be renamed to e.g. python_old.exe. After that, run your .py file and it should fail to find the python.exe. Next, set up Open With to point to e.g. C:\Python39\python.exe and that'll fix the issue. You can then rename python_old.exe back to python.exe under C:\Python27. – shadowz1337 Mar 29 '23 at 05:51
16

See here for original post

;
; This is an example of how a Python Launcher .ini file is structured.
; If you want to use it, copy it to py.ini and make your changes there,
; after removing this header comment.
; This file will be removed on launcher uninstallation and overwritten
; when the launcher is installed or upgraded, so don't edit this file
; as your changes will be lost.
;
[defaults]
; Uncomment out the following line to have Python 3 be the default.
;python=3

[commands]
; Put in any customised commands you want here, in the format
; that's shown in the example line. You only need quotes around the
; executable if the path has spaces in it.
;
; You can then use e.g. #!myprog as your shebang line in scripts, and
; the launcher would invoke e.g.
;
; "c:\Program Files\MyCustom.exe" -a -b -c myscript.py
;
;myprog="c:\Program Files\MyCustom.exe" -a -b -c

Thus, on my system I made a py.ini file under c:\windows\ where py.exe exists, with the following contents:

[defaults]
python=3

Now when you Double-click on a .py file, it will be run by the new default version. Now I'm only using the Shebang #! python2 on my old scripts.

Ehsan Iran-Nejad
  • 1,697
  • 1
  • 15
  • 20
9
  1. Edit registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe\default
  2. Set default program to open .py files to python.exe
Druid
  • 6,423
  • 4
  • 41
  • 56
cuble
  • 306
  • 2
  • 7
9

This work for me.

If you want to use the python 3.6 you must move the python3.6 on the top of the list.

The same applies to the python2.7 If you want to have the 2.7 as default then make sure you move the python2.7 on the very top on the list.

step 1

enter image description here

step 2

enter image description here

step 3

enter image description here

then close any cmd command prompt and opened again, it should work as expected.

python --version

>>> Python 3.6
George C.
  • 6,574
  • 12
  • 55
  • 80
6

This worked for me:

Go to

Control Panel\System and Security\System

select

Advanced system settings from the left panel
from Advanced tab click on Environment Variables

In the System variables section search for (create if doesn't exist)

PYTHONPATH

and set

C:\Python27\;C:\Python27\Scripts;

or your desired version

You need to restart CMD.

In case it still doesn't work you might want to leave in the PATH variable only your desired version.

George B
  • 424
  • 1
  • 7
  • 17
6

With Python versions 2.7, 3.7, 3.9, and 3.11 installed on my Windows 11 OS, I encountered issues with the previously suggested solutions. However, I found a straightforward method that worked for me.

First, let's understand how to set the default Python version using the py command. Running py --help provides hints, including a reference to the py.ini file located in %LOCALAPPDATA%\py.ini. This file allows us to specify the default Python version.

To set a specific default Python version:

  1. Open a text editor or create a file in %LOCALAPPDATA%\py.ini.

  2. Add the following content to the py.ini file:

[defaults]
python=3.7
  1. Save the file.

Now, when you run py --version in the console, it should display the specified default Python version.

Please note the following additional points to consider:

  • Purpose of setting the default Python version: Setting a specific default Python version can be helpful for ensuring compatibility with certain dependencies or maintaining consistency across projects.

  • Location of %LOCALAPPDATA%: The %LOCALAPPDATA% environment variable typically refers to C:\Users\<username>\AppData\Local. If the py.ini file does not exist in that location, you can create it manually.

flydev
  • 4,327
  • 2
  • 31
  • 36
  • work with 3.9 but not with 3.11 (related: https://stackoverflow.com/questions/68121982/multiple-python-versions-installed-how-to-set-the-default-version-for-py-exe/68139696#68139696 ) – JinSnow Nov 19 '22 at 17:11
  • 2
    @JinSnow by setting `python=3.11` I get `C:\Users\bob>py --version` `Python 3.11.0` - it's also possible that I don't get the point of your comment. – flydev Nov 25 '22 at 15:29
2

Now that Python 3.3 is released it is easiest to use the py.exe utility described here: http://www.python.org/dev/peps/pep-0397/

It allows you to specify a Python version in your script file using a UNIX style directive. There are also command line and environment variable options for controlling which version of Python is run.

The easiest way to get this utility is to install Python 3.3 or later.

Gerald
  • 609
  • 5
  • 13
2

Nothing above worked, this is what worked for me:

ftype Python.File=C:\Path\to\python.exe "%1" %*

This command should be run in Command prompt launched as administrator

Warning: even if the path in this command is set to python35, if you have python36 installed it's going to set the default to python36. To prevent this, you can temporarily change the folder name from Python36 to xxPython36, run the command and then remove the change to the Python 36 folder.


Edit: This is what I ended up doing: I use Python Launcher. https://stackoverflow.com/a/68139696/3154274

MagTun
  • 5,619
  • 5
  • 63
  • 104
2

If you are on Windows, use the ASSOC command to change the default python version for python programs.

assoc .py=<Python 3.1 directory>
DerpyCoder
  • 127
  • 1
  • 6
1

Check which one the system is currently using:

python --version

Add the main folder location (e.g. C/ProgramFiles) and Scripts location (C/ProgramFiles/Scripts) to Environment Variables of the system. Add both 3.x version and 2.x version

Path location is ranked inside environment variable. If you want to use Python 2.x simply put path of python 2.x first, if you want for Python 3.x simply put 3.x first

This uses python 2

pranavhd
  • 25
  • 7
1

I had same problem and solve it by executing the installation file again. when you do that python automatically knows you have installed it before so it recommends you 3 options! select modify and select all packages you want to modify then in the next page you can check if new version of python is added to your environment variables or not. check it and then execute modification. I did and it solved.

0

Since my problem was slightly different and none of the above worked for me, I'll add what worked for me. I had installed the new python launcher for python 3.10 today, installed the version through it, but the command window did not recognise the version. Instead, it listed older python3 versions I had on my computer.

Finally, in the windows programs list, I saw that I had two versions of the python launcher. I uninstalled the old one, and now python 3.10 shows up correctly when running py -0 and is the chosen version when running py.

Apologies if this is a noob answer, I am new to all this.

Draenth
  • 1
  • 1
0

Set PY_PYTHON environment variable as 2.6 (or any version you want). Restart the terminal or cmd and type py -0p. The 2.6 should have a * next to it indicating that's the default python version now.

vinodhraj
  • 177
  • 1
  • 7
-1

Use SET command in Windows CMD to temporarily set the default python for the current session.

SET PATH=C:\Program Files\Python 3.5
ron4ex
  • 1,073
  • 10
  • 21
-4

Try modifying the path in the windows registry (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment).

Caveat: Don't break the registry :)

phooji
  • 10,086
  • 2
  • 38
  • 45
  • I have already set the environment var correctly,for my question,it doesn't work in the bat file – rooney Feb 23 '11 at 07:07
  • 3
    thank you anyway!I find the right way,I modify the value of the registry item in HKEY_LOCAL_CLASS\Applications\Python.exe\shell\open\command,and then it works right for bat file – rooney Feb 23 '11 at 08:18