How to run different python versions in cmd

164,668

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:

  1. download python2x and python3x, installed them.
  2. add C:\Python35;C:\Python35\Scripts;C:\Python27;C:\Python27\Scripts to environment variable PATH.
  3. Go to C:\Python35 to rename python.exe to python3.exe, also to C:\Python27, rename python.exe to python2.exe.
  4. restart your command window.
  5. type python2 scriptname.py, or python3 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.

Share:
164,668
hamidfzm
Author by

hamidfzm

Developer interested in programming with Go, Python, Javascript, and C

Updated on July 28, 2022

Comments

  • hamidfzm
    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 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.

  • pepr
    pepr over 10 years
    +1 for being faster and for including the link.
  • hamidfzm
    hamidfzm over 10 years
    Please explain more I couldn't understand what you have said!
  • pepr
    pepr over 10 years
    You 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.
  • laike9m
    laike9m about 10 years
    Amazing, amazing, amazing!!
  • BlackJack
    BlackJack over 8 years
    @pepr Renaming the exe breaks pip for that installation as it expects the executable under its original name.
  • pepr
    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
    martineau about 7 years
    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?
  • Chris Gong
    Chris Gong over 6 years
    Thanks a lot, using the py command after installing a Python 3.3 and up version worked!
  • pepr
    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
    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
    alansiqueira27 over 5 years
    what if I want to execute something like "pytest file.py"? It does not work like "python2 pytest file.py"
  • Admin
    Admin over 5 years
    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.
  • pepr
    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
    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
    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 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.
  • Admin
    Admin over 5 years
    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.
  • pepr
    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 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.
  • johan d
    johan d almost 5 years
    I think it's important to add that we can specify the minor version also, with eg. #!python3.7
  • pepr
    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
    johan d almost 5 years
    To 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.