Categories
compatibility python windows

How to run multiple Python versions on Windows

233

I had two versions of Python installed on my machine (versions 2.6 and 2.5). I want to run 2.6 for one project and 2.5 for another.

How can I specify which I want to use?

I am working on Windows XP SP2.

4

171

Running a different copy of Python is as easy as starting the correct executable. You mention that you’ve started a python instance, from the command line, by simply typing python.

What this does under Windows, is to trawl the %PATH% environment variable, checking for an executable, either batch file (.bat), command file (.cmd) or some other executable to run (this is controlled by the PATHEXT environment variable), that matches the name given. When it finds the correct file to run the file is being run.

Now, if you’ve installed two python versions 2.5 and 2.6, the path will have both of their directories in it, something like PATH=c:\python\2.5;c:\python\2.6 but Windows will stop examining the path when it finds a match.

What you really need to do is to explicitly call one or both of the applications, such as c:\python\2.5\python.exe or c:\python\2.6\python.exe.

The other alternative is to create a shortcut to the respective python.exe calling one of them python25 and the other python26; you can then simply run python25 on your command line.

16

  • 69

    how to create that shortcut

    Jan 4, 2011 at 6:11

  • 9

    When you’re in windows, navigate to the folder that contains the python version you want to create a shortcut for, then right click and create shortcut. You can then rename it.

    – aodj

    Jan 4, 2011 at 10:29

  • 4

    Sorry to dig up a long dead post, but how will you make the shortcut work without requiring the .lnk extension?

    Oct 30, 2011 at 20:52

  • 8

    If a shortcut doesn’t work, you can do as @F.J said, and simply copy and rename. Failing that, you can make a symbolic link, using ”mklink” on the command line.

    – aodj

    Nov 13, 2011 at 13:34

  • 8

    Wouldn’t the best way be to create a bat file called python25 and python26 and make those call the appropriate version? Then all you would need to do is put those 2 bat files alongside their binaries.

    Jan 29, 2013 at 16:05

141

Adding two more solutions to the problem:

  • Use pylauncher (if you have Python 3.3 or newer there’s no need to install it as it comes with Python already) and either add shebang lines to your scripts;

#! c:\[path to Python 2.5]\python.exe – for scripts you want to be run with Python 2.5
#! c:\[path to Python 2.6]\python.exe – for scripts you want to be run with Python 2.6

or instead of running python command run pylauncher command (py) specyfing which version of Python you want;

py -2.6 – version 2.6
py -2 – latest installed version 2.x
py -3.4 – version 3.4
py -3 – latest installed version 3.x

virtualenv -p c:\[path to Python 2.5]\python.exe [path where you want to have virtualenv using Python 2.5 created]\[name of virtualenv]

virtualenv -p c:\[path to Python 2.6]\python.exe [path where you want to have virtualenv using Python 2.6 created]\[name of virtualenv]

for example

virtualenv -p c:\python2.5\python.exe c:\venvs\2.5

virtualenv -p c:\python2.6\python.exe c:\venvs\2.6

then you can activate the first and work with Python 2.5 like this
c:\venvs\2.5\activate
and when you want to switch to Python 2.6 you do

deactivate  
c:\venvs\2.6\activate

6

  • 1

    pylauncher appears to be a prototype implementation of PEP 397 which was Accepted as Standards Track way back in 2011. Do you know why the launcher still isn’t being distributed with Python for Windows or why there’s still only Vinay Sajip’s prototype implementation?

    – martineau

    Jan 2, 2013 at 4:58

  • 4

    Pylauncher is being distributed with Python starting from version 3.3 – see python.org/download/releases/3.3.0. Also I think Vinay Sajip’s implementation is the implementation not merely a prototype.

    Jan 2, 2013 at 10:02

  • 1

    Thank you for the clarification. IMHO pylauncher should be distributed as part of the latest Python 2 version too because people using that version are more likely to the ones wanting to install multiple versions (and be more likely to do so if they were aware of its functionality and availability).

    – martineau

    Jan 2, 2013 at 16:42

  • StackOverflow should allow multiple upvotes, your answer deserves infinite upvotes! Seriously where have you been bro! 😀 Simple, Clear and working of course!

    – 3bdalla

    Jul 13, 2015 at 14:32

  • 2

    Slightly nicer: Write the shebang lines UNIX-style; the launcher knows how to parse them. So #!/usr/bin/env python2.7 will find the latest 2.7 interpreter installed when run with py.exe, with no additional arguments required.

    Jan 28, 2021 at 3:32

60

From Python 3.3 on, there is the official Python launcher for Windows (http://www.python.org/dev/peps/pep-0397/). Now, you can use the #!pythonX to determine the wanted version of the interpreter also on Windows. See more details in my another comment or read the PEP 397.

Summary: The py script.py launches the Python version stated in #! or Python 2 if #! is missing. The py -3 script.py launches the Python 3.

9

  • 2

    This is the answer I’m looking for. I run Windows 10 with Python 2.7 and Python 3.4.3. In command prompt type in “py [python_version_number]” ex: py -3 or py will invoke the python version you have. I think environment variables must be set before you use this. this is convenient for me.

    Mar 12, 2016 at 12:57


  • 3

    @Inuka: No environment variables need to be set. The Python installer sets the associations with the .py extension. The launcher itself is installed into C:\Windows that is already in the PATH. This way, also the PATH variable need not to be modified.

    – pepr

    Mar 14, 2016 at 12:49

  • 3

    Thanks a lot for your answer mate. From this way we can invoke the pip as well. py -2 -m pip install SomePackage or py -3.4.3 -m pip install SomePackage

    Mar 14, 2016 at 17:15

  • 1

    I think this the most simple and no fuss solution.

    – prasad

    May 19, 2016 at 19:39

  • Thanks! the comments have been really helpful. How do I activate a virtual environment with one of the python versions I have using pipenv?

    Sep 15, 2020 at 8:31