How can I configure windows command dialog to run different python versions in it? For example when I type python2
it runs python 2.7 and when I type python3
it runs python 3.3? I know how to configure environment variables for one version but two? I mean something like Linux terminal.

- 4,595
- 8
- 48
- 80
3 Answers
I also met the case to use both python2 and python3 on my Windows machine. Here's how i resolved it:
- download python2x and python3x, installed them.
- add
C:\Python35;C:\Python35\Scripts;C:\Python27;C:\Python27\Scripts
to environment variablePATH
. - Go to
C:\Python35
to renamepython.exe
topython3.exe
, also toC:\Python27
, renamepython.exe
topython2.exe
. - restart your command window.
- type
python2 scriptname.py
, orpython3 scriptname.py
in command line to switch the version you like.

- 82,685
- 13
- 120
- 165

- 836
- 1
- 7
- 5
-
To which environment variable did you add all the Python paths? As for renaming the `python.exe` files, how does doing that affect things when doing minor upgrades, say from version 2.7.x to 2.7.x+y, which normally would both be located in the same `C:\Python27` directory? – martineau Feb 13 '17 at 03:49
-
what if I want to execute something like "pytest file.py"? It does not work like "python2 pytest file.py" – alansiqueira27 Sep 11 '18 at 13:44
Python 3.3 introduces Python Launcher for Windows that is installed into c:\Windows\
as py.exe
and pyw.exe
by the installer. The installer also creates associations with .py
and .pyw
. Then add #!python3
or #!python2
as the first lline. No need to add anything to the PATH
environment variable.
Update: Just install Python 3.3 from the official python.org/download. It will add also the launcher. Then add the first line to your script that has the .py
extension. Then you can launch the script by simply typing the scriptname.py
on the cmd line, od more explicitly by py scriptname.py
, and also by double clicking on the scipt icon.
The py.exe
looks for C:\PythonXX\python.exe
where XX
is related to the installed versions of Python at the computer. Say, you have Python 2.7.6 installed into C:\Python27
, and Python 3.3.3 installed into C:\Python33
. The first line in the script will be used by the Python launcher to choose one of the installed versions. The default (i.e. without telling the version explicitly) is to use the highest version of Python 2 that is available on the computer.

- 20,112
- 15
- 76
- 139
-
5
-
8You can always rename your `C:\Python33\python.exe` to the `C:\Python33\python3.exe`. However, it is better to use explicitly `py -3 scriptname.py`. It is even better to put the information into the script, and then launch your scripts for either version the uniform way. – pepr Dec 27 '13 at 08:14
-
@pepr Renaming the exe breaks `pip` for that installation as it expects the executable under its original name. – BlackJack Jan 19 '16 at 16:40
-
1@BlackJack: I see. Anyway, you can create a new copy named `python3.exe`. However, using the Python Launcher for Windows is better, and there is no need for doing such things. – pepr Jan 20 '16 at 17:09
-
Thanks a lot, using the `py` command after installing a Python 3.3 and up version worked! – Chris Gong Jan 28 '18 at 16:03
-
@ChrisGong: The newer versions of Python changed the default. Now `py` implicitly launches Python 3 if not expressed explicitly otherwise. – pepr Jan 29 '18 at 20:21
-
@pepr ah yes, i forgot to mention that it worked when I specified `#!python2` in the first line of my script – Chris Gong Jan 29 '18 at 21:13
-
Why would I want to modify my scripts? That defeats the purpose especially when its a large collection of scripts I just downloaded from git. Hacking the environment variables and logging in again is as good as we can go IMHO. – Jan 10 '19 at 16:21
-
@ConradB: How would you hack your environment variables if you have both Python 2 and Python 3 scripts? Or you have to use the correct executable explicitly (then you do not need any tweaking), or you have to store that information in the script. – pepr Jan 13 '19 at 15:02
-
@pepr I am setting my windows path, then logging out and logging in again. I removed python from the SYSTEM path and added it to my USER path instead. I hardly ever have to switch python versions, so it's not a huge effort. I am unable to use explicitly, because some scripts spawn interpreter. – Jan 14 '19 at 17:42
-
3@ConradB OK. But the question is how to run different Python version in `cmd`. When using Python Launcher for Windows, the Python is not in the path at all -- nor the system, nor the user paths. That is because py.exe is in `c:\Windows`. And if you do not want to modify the script, you can always call `py -3 myscript.py` for Python 3 or `py -2 myscript.py` for Python 2. Default today for `py myscript.py` uses Python 3 interpreter. – pepr Jan 14 '19 at 19:18
-
Sorry, @pepr I answered a different question, true, but normally python.exe is installed into one folder per version c:\python27\python.exe or c:\python37\python.exe - If you run py.exe you get the the last one you installed into c:\windows, which is a somewhat evil place to put a binary, and probably not to be relied upon on a secured computer. Hope that helps even though I did misslead. – Jan 16 '19 at 16:23
-
@ConradB: No, this is not the case. The Python Launcher for Windows can actually be seen as the replacement of the missing Unix feature to be able to prescribe the interpreter in the script. The `py.exe` is a single program in the `c:\Windows`, but it searches for all installed versions of Python on the computer. Based on the `#!` line in the script or using the explicit parameter of `py.exe` it finds and calls the needed version of Python. Placing it (by admin) to `c:\Windows` is not more dangerous than having other programs there. – pepr Jan 21 '19 at 20:03
-
I think it's important to add that we can specify the minor version also, with eg. `#!python3.7` – johan d May 19 '19 at 00:34
-
@johand.: Actually no. You can tell also the minor version if you need to be specific. However, the major version is enough. It will use the highest minor version that is installed. – pepr May 20 '19 at 08:34
-
2To quote myself, " we can specify the minor " -> it is possible, and sometimes even necessary. As an example, my own config hosts a 3.6 and a 3.7 version, and I have a lib that is not (yet?) compatible with 3.7. I fixed my issues by specifying a compatible minor version. – johan d May 23 '19 at 03:35
I would suggest using the Python Launcher for Windows utility that was introduced into Python 3.3. You can manually download and install it directly from the author's website for use with earlier versions of Python 2 and 3.
Regardless of how you obtain it, after installation it will have associated itself with all the standard Python file extensions (i.e. .py,
.pyw
, .pyc
, and .pyo
files). You'll not only be able to explicitly control which version is used at the command-prompt, but also on a script-by-script basis by adding Linux/Unix-y shebang #!/usr/bin/env pythonX
comments at the beginning of your Python scripts.

- 119,623
- 25
- 170
- 301