How to run multiple Python versions on Windows

302,226

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

As per @alexander you can make a set of symbolic links like below. Put them somewhere which is included in your path so they can be easily invoked

> cd c:\bin
> mklink python25.exe c:\python25\python.exe
> mklink python26.exe c:\python26\python.exe

As long as c:\bin or where ever you placed them in is in your path you can now go

> python25

Solution 5

For example for 3.6 version type py -3.6. If you have also 32bit and 64bit versions, you can just type py -3.6-64 or py -3.6-32.

Share:
302,226

Related videos on Youtube

Bilal Basharat
Author by

Bilal Basharat

Updated on October 22, 2021

Comments

  • Bilal Basharat
    Bilal Basharat over 2 years

    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.

    • Lennart Regebro
      Lennart Regebro over 13 years
      Good hints for asking questions is to explain what you have tried, and in what way that failed. So: What did you try, and in what way did that not work?
  • Bilal Basharat
    Bilal Basharat over 13 years
    currently i am working on 2.5. simply elaborate me how can i run 2.6 also. in windows command prompt i had to write 'python' to enter python shell. and it is 2.5. in c drive i had both version installed.
  • Bilal Basharat
    Bilal Basharat over 13 years
    thanks for ure help plz answer my second question also: how can i run both at a time? as i successfully switched from 2.5 to 2.6
  • Lennart Regebro
    Lennart Regebro over 13 years
    @Bilal Basharat: Windows is a multi-tasking OS if you want to run two things at the same time, you just start both, so I don't understand your question.
  • albertov
    albertov over 13 years
    Instead of python to enter the shell try python2.5 or python2.6. I'm not a windows user, but on unix /usr/bin/python is usually an alias to the fully qualified executable, I'm assuming python in windows is installed in a similar manner
  • Bilal Basharat
    Bilal Basharat over 13 years
    currently i am working on python2.5. simply elaborate me how can i run 2.6 also ? when i write 'python' in windows command prompt to enter python shell than python2.5 appears. in c drive i had both version installed. when i go to C:\Python2.6\Python.exe. than python2.6 temporarily activated. as soon as i leave C:\Python2.6\ it again turned into version 2.5
  • Lennart Regebro
    Lennart Regebro over 13 years
    @Bilal Basharat: I already answered this. It is not "temporarily activated". You ran Python 2.6 with the command C:\Python2.6\Python.exe and that is how you run it. And you run Python 2.5 with the command C:\Python2.5\Python.exe. That is how you run both at the same time.
  • jgritty
    jgritty over 13 years
    On windows the executable name is always python.exe, but you change path to the executable. It's been explained several times already though.
  • martineau
    martineau over 13 years
    @Bilal Basharat: If either the C:\Python2.5 or C:\Python2.6 directory appears in your PATH environment variable, the corresponding version of Python will become the default unless you override it by explicitly specifying a different path to the .exe you wish to use.
  • Bilal Basharat
    Bilal Basharat over 13 years
    how to create that shortcut
  • aodj
    aodj over 13 years
    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.
  • Nathan Tornquist
    Nathan Tornquist over 12 years
    Sorry to dig up a long dead post, but how will you make the shortcut work without requiring the .lnk extension?
  • Andrew Clark
    Andrew Clark over 12 years
    @NathanTornquist - I was just trying to figure out the same problem, the best I could come up with is making copies of the executables and renaming them instead of creating shortcuts.
  • aodj
    aodj over 12 years
    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.
  • Nathan Tornquist
    Nathan Tornquist over 12 years
    @F. J. I ended up creating shortcuts for the python files with the names that I wanted to show up in command prompt, then I added the .lnk extension to the path so that shortcuts worked as commands in command prompt.
  • Sebastian Graf
    Sebastian Graf over 11 years
    The solution would be to use hard links using the /H switch of mklink. You can't just copy and paste the links that way though.
  • martineau
    martineau over 11 years
    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
    martineau over 11 years
    Clever idea. BTW mklink is only natively available in Windows Vista/2008+. On XP and Server 2003 a "hardlink" could be created instead using fsutil hardlink create <new filename> <existing filename> and putting or moving the <new filename> to somewhere in your path. Hardlinks only work on the same drive, however.
  • Piotr Dobrogost
    Piotr Dobrogost over 11 years
    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.
  • martineau
    martineau over 11 years
    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).
  • java_newbie
    java_newbie over 11 years
    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.
  • meawoppl
    meawoppl over 10 years
    This is a good solution, I am not sure it works without a NTFS based file system as well.
  • meawoppl
    meawoppl over 10 years
    The mklink solution is posted a couple answers down the page. I am using it and have been very happy with it.
  • Christopher Hackett
    Christopher Hackett over 10 years
    Yes, requires support for NTFS symbolic links which I believe was introduced in Vista (I may be wrong). Use on XP requires use of a different driver. Sounds like it was turned off at some stage before general release.
  • 3bdalla
    3bdalla almost 9 years
    StackOverflow should allow multiple upvotes, your answer deserves infinite upvotes! Seriously where have you been bro! :D Simple, Clear and working of course!
  • Tycon
    Tycon almost 9 years
    You can add .ink to the environment variable PATHTEXT to avoid typing ink. Or, you can just add .exe (case sensitive), copy python.exe as python34.exe.
  • CodeMonkey
    CodeMonkey about 8 years
    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.
  • pepr
    pepr about 8 years
    @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.
  • CodeMonkey
    CodeMonkey about 8 years
    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
  • prasad
    prasad almost 8 years
    I think this the most simple and no fuss solution.
  • Gregor
    Gregor almost 7 years
    this worked for me. Just have to use copy instead of cp. Also when running this command you have to be somewhere on the path and both python installations e.g. c:\python34\bin\python.exe and c:\python27\bin\python.exe need to be in the path also . ( preferably in that order).
  • giantas
    giantas over 6 years
    easiest best solution
  • Xonshiz
    Xonshiz over 4 years
    This should be the Accepted Answer! Thanks a ton!
  • yeahman
    yeahman over 4 years
    how can I install libraries with pip for a particular version of python??
  • Enchant97
    Enchant97 over 4 years
    This is possibly the best answer, as it requires no renaming of files and third party IDEs will detect both as normal!
  • testuser
    testuser over 4 years
    I think, more or less this is the practical solution for the question. This is more handy compared with the approach where shortcut / symbolic link is used to call the python script. Instead of renaming the file, we can copy & paste the binary (python.exe) then rename it according to it's version (as shown in this answer). By the way, python command would be confusing to OS.
  • Alaa M.
    Alaa M. over 4 years
    @JeremyCantrell - Good solution for eliminating the need for the extension, but how can you run a .py file using that? If you type python35 myfile.py, it will go into the python console and ignore myfile.py
  • Dave Marley
    Dave Marley about 4 years
    Very good suggestions. I used the second (virtualenv) one. Only difference I found was I had to include '\scripts' in the path to activate, eg: c:\venvs\2.5\scripts\activate. This is on Windows 10 if that makes a difference. Other than that, works like a charm, thanks.
  • java_newbie
    java_newbie about 4 years
    @AlaaM. Not if you pass the command line arguments to the python command in the script using %*
  • Alaa M.
    Alaa M. almost 4 years
    @JeremyCantrell - Can you elaborate on this? How do you run the script using the bat file?
  • Ed Randall
    Ed Randall almost 4 years
    could you enhance the answer with more explanation - where is this py executable - is it a windows-only addition? Is it possible to incorporate this into a cross-platform command-line script which has at the top: #!/usr/bin/env python3 for example?
  • Bright Chang
    Bright Chang almost 4 years
    Great answer! Thanks~@山茶树和葡萄树
  • java_newbie
    java_newbie almost 4 years
    @AlaaM. Inside the bat file, you'll need to call the python executable like this: c:\path\to\python.exe %*. The %* causes any arguments given to the bat file to be passed along to the python interpreter.
  • omokehinde igbekoyi
    omokehinde igbekoyi over 3 years
    Thanks! the comments have been really helpful. How do I activate a virtual environment with one of the python versions I have using pipenv?
  • omokehinde igbekoyi
    omokehinde igbekoyi over 3 years
    I used pipenv --python 3.7 to set the python version
  • Shilan
    Shilan over 3 years
    how can I then run for example jupyter notebook with version 3.8, when I have both 3.7 and 3.8?
  • Angel Auñón
    Angel Auñón over 3 years
    @Shilan you can proceed the same way as when calling pip when calling jupyter: py -3.8 -m jupyter notebook
  • Cerberton
    Cerberton over 3 years
    This is much easier than the other suggestions.
  • Shilan
    Shilan over 3 years
    @AngelAuñón but I only type “jupyter notebook” in the command line and it starts. I am not sure if it works if I run py ...
  • ShadowRanger
    ShadowRanger over 3 years
    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.
  • David C.
    David C. about 3 years
    Perfect. I just tested it on my system, installing versions 3.6.13, 3.8.7 and 3.9.2. If I type py foo.py, it runs the latest version I have installed. I can type py -3.8 foo.py or py -3.6 foo.py to select one of the others.
  • pepr
    pepr about 3 years
    @DavidC: Yes, the later versions launches the latest Python 3 version that is installed. At the time, Python 3 was rather new, and the latest Python 2 was the default to be launched.
  • Ray Depew
    Ray Depew about 3 years
    @EdRandall my response is a bit late, but py is Py Launcher for Windows. Official documentation is here: docs.python.org/3/using/… and it includes support for shebangs.
  • Ray Depew
    Ray Depew about 3 years
    An example, to create a virtual environment that runs a specific (installed) version of Python: py -3.7 -m venv myEnvs\three7
  • David C.
    David C. almost 3 years
    This is a mimic to the way it is done on Unix systems - multiple python executables with version-specific names. But it requires hacking the installation on Windows. The Python launcher (see other comments) is easier for the rest of us - especially those not familiar with Unix installations.
  • Vyk
    Vyk over 2 years
    py -2 script.py worked for me.
  • Vinícius Queiroz
    Vinícius Queiroz over 2 years
    Simply creating a shortcut as @aodj recommended did not work for me. I followed these instructions here. Namely, the "Copy the Executable File" section (and the one right before it, to change directories in PowerShell) did something else that was also required to make a copy of the executable with a different name. Works perfectly now. (Don't forget to setup your PATH as well)