How to run different python versions in cmd
Solution 1
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.
Solution 2
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.
Solution 3
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.
hamidfzm
Developer interested in programming with Go, Python, Javascript, and C
Updated on July 28, 2022Comments
-
hamidfzm almost 2 years
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 typepython3
it runs python 3.3? I know how to configure environment variables for one version but two? I mean something like Linux terminal. -
pepr over 10 years+1 for being faster and for including the link.
-
hamidfzm over 10 yearsPlease explain more I couldn't understand what you have said!
-
pepr over 10 yearsYou can always rename your
C:\Python33\python.exe
to theC:\Python33\python3.exe
. However, it is better to use explicitlypy -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. -
laike9m about 10 yearsAmazing, amazing, amazing!!
-
BlackJack over 8 years@pepr Renaming the exe breaks
pip
for that installation as it expects the executable under its original name. -
pepr over 8 years@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. -
martineau about 7 yearsTo 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 sameC:\Python27
directory? -
Chris Gong over 6 yearsThanks a lot, using the
py
command after installing a Python 3.3 and up version worked! -
pepr over 6 years@ChrisGong: The newer versions of Python changed the default. Now
py
implicitly launches Python 3 if not expressed explicitly otherwise. -
Chris Gong over 6 years@pepr ah yes, i forgot to mention that it worked when I specified
#!python2
in the first line of my script -
alansiqueira27 over 5 yearswhat if I want to execute something like "pytest file.py"? It does not work like "python2 pytest file.py"
-
Admin over 5 yearsWhy 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.
-
pepr over 5 years@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.
-
Admin over 5 years@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.
-
pepr over 5 years@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 inc:\Windows
. And if you do not want to modify the script, you can always callpy -3 myscript.py
for Python 3 orpy -2 myscript.py
for Python 2. Default today forpy myscript.py
uses Python 3 interpreter. -
Admin over 5 yearsSorry, @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.
-
pepr over 5 years@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 thec:\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 ofpy.exe
it finds and calls the needed version of Python. Placing it (by admin) toc:\Windows
is not more dangerous than having other programs there. -
johan d almost 5 yearsI think it's important to add that we can specify the minor version also, with eg.
#!python3.7
-
pepr almost 5 years@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.
-
johan d almost 5 yearsTo 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.