How to run different python versions in cmd

Question:

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.

Asked By: hamidfzm

||

Answers:

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.

Answered By: martineau

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:PythonXXpython.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.

Answered By: pepr

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:Python35Scripts;C:Python27;C:Python27Scripts 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.
Answered By: Leung Ying Ying