187

I'm running Windows and the shell/OS automatically runs Python based on the registry settings when you run a program on the command line. Will this break if I install a 2.x and 3.x version of Python on the same machine?

I want to play with Python 3 while still being able to run 2.x scripts on the same machine.

alex
  • 6,818
  • 9
  • 52
  • 103
minty
  • 22,235
  • 40
  • 89
  • 106
  • 1
    In my distro, Fedora, it installs Python 2.7 at `/usr/bin/python` and Python 3.3 at `/usr/bin/python3`. Gives different names for Python3's Pip and IPython too. Very handy. – Colonel Panic Oct 24 '13 at 15:43
  • 20
    @user - In spirit I agree with your response, but I've been burned by that spirit so many times that I understand why someone would ask before making the leap. – Peter Hanley Apr 24 '14 at 19:59
  • The answers here seem to be all about how to choose which python to run when. My problem was that the installer (3.6.2) wouldn't run because it says "Another version of this product is already installed." Turns out I already had 3.6.3 installed. – yoyo Jan 24 '18 at 01:02
  • Here are some step by step instructions: https://datascience.com.co/how-to-install-python-2-7-and-3-6-in-windows-10-add-python-path-281e7eae62a – Abdalla Roda Aug 24 '22 at 03:58

20 Answers20

75

The official solution for coexistence seems to be the Python Launcher for Windows, PEP 397 which was included in Python 3.3.0. Installing the release dumps py.exe and pyw.exe launchers into %SYSTEMROOT% (C:\Windows) which is then associated with py and pyw scripts, respectively.

In order to use the new launcher (without manually setting up your own associations to it), leave the "Register Extensions" option enabled. I'm not quite sure why, but on my machine it left Py 2.7 as the "default" (of the launcher).

Running scripts by calling them directly from the command line will route them through the launcher and parse the shebang (if it exists). You can also explicitly call the launcher and use switches: py -3 mypy2script.py.

All manner of shebangs seem to work

  • #!C:\Python33\python.exe
  • #!python3
  • #!/usr/bin/env python3

as well as wanton abuses

  • #! notepad.exe
Nick T
  • 25,754
  • 12
  • 83
  • 121
  • "They" really ought to include pylauncher with the current Python 2 distribution for Windows (or make people more aware it's available and where to get it themselves). – martineau Jan 02 '13 at 05:40
  • 1
    Perhaps, but if you're just living in the Python 2 world it's not as-big of a deal. – Nick T May 30 '13 at 03:17
  • This launcher was available as standalone program at least since mid 2012. – Smit Johnth Jun 18 '16 at 19:28
  • 3
    Update: [As of 3.6, `py.exe` should default to launching Py3 over Py2 when no version is specified and the file contains no shebang line](https://docs.python.org/3/whatsnew/3.6.html#summary-release-highlights) – ShadowRanger Mar 10 '17 at 02:29
  • How do I use `pip` in this case? Using `pip` might install for Python 3. How to specify the version while installing? – Mooncrater Oct 19 '17 at 09:40
  • 1
    @Mooncrater you could check which will be used with `where.exe pip`. I think the Python 3 installer installs a `pip3` alias for its pip, but you could also do something like `py -3 -m pip install requests` I think. – Nick T Oct 19 '17 at 15:25
  • Okay. Thanks @NickT – Mooncrater Oct 19 '17 at 17:30
  • In order to launch a python2 script, the shebang needs to be `#!python2` or `#!/usr/bin/env python2`. I had some veeery old scripts which had `#!/usr/bin/env python` and it was launching them with Python3. – gog Nov 06 '19 at 09:59
60

Here's my setup:

  1. Install both Python 2.7 and 3.4 with the windows installers.
  2. Go to C:\Python34 (the default install path) and change python.exe to python3.exe
  3. Edit your environment variables to include C:\Python27\;C:\Python27\Scripts\;C:\Python34\;C:\Python34\Scripts\;

Now in command line you can use python for 2.7 and python3 for 3.4.

Alistair Martin
  • 742
  • 5
  • 5
  • 4
    What about, for example, `pip` in cli? – Christian Nov 29 '15 at 16:21
  • 3
    The latest versions of python 2 & 3 ship with pip. So you can use `pip` and `pip3` respectively. – Alistair Martin Nov 30 '15 at 23:39
  • 8
    I was merely pointing out that you'd end up having to rename all these utilities. Even then, there's a risk that things break if, for example, python3 explicitly (eg: hard coded) calls pip and not pip3. – Christian Dec 01 '15 at 18:46
  • That's a bad idea, would end up renaming everything and pip doesn't work that way. – bhansa Jun 29 '17 at 07:40
  • 2
    to stop running into pip issue on the python3. you need UNSLECT "install pip" from the initial python3 instllation wizard, then rename python3.7/python.exe to python3.exe, THEN download get-pip.py and run python3.exe get-pip.py. So the pip3 knows the renamed python3.exe is for python3 not the origional /python3.7/python.exe – Junchen Liu Jun 09 '19 at 18:35
  • You also need to update the install locations in the registry editor. – otoomey Dec 15 '19 at 16:56
46

From version 3.3 Python introduced Launcher for Windows utility https://docs.python.org/3/using/windows.html#python-launcher-for-windows.

So to be able to use multiple versions of Python:

  1. install Python 2.x (x is any version you need)
  2. install Python 3.x (x is any version you need also you have to have one version 3.x >= 3.3)
  3. open Command Prompt
  4. type py -2.x to launch Python 2.x
  5. type py -3.x to launch Python 3.x
Ivan Kucerak
  • 591
  • 4
  • 6
  • 1
    What about people who use IDE's? Does this work for that too? – emorphus Jul 24 '16 at 16:54
  • 1
    @emorphus I guess depends on IDE but in general you should be able to choose version of interpreter. So in case you have multiple Python versions installed just choose the one you want to use in your project. Also terminal in IDE works same as command prompt so those commands should work there too. – Ivan Kucerak Jul 25 '16 at 08:09
  • 2
    This is the best solution IMO, it works perfectly from the get-go. Don't need to have -3.x you can just do `py -3 filename.py`. – OmegaNalphA Feb 15 '18 at 19:37
36

You can have both installed.

You should write this in front of your script:

#!/bin/env python2.7

or, eventually...

#!/bin/env python3.6

Update

My solution works perfectly with Unix, after a quick search on Google, here is the Windows solution:

#!c:/Python/python3_6.exe -u

Same thing: in front of your script.

AtilioA
  • 419
  • 2
  • 6
  • 19
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
  • WEll, it can be installed on Linux, and can be on Windows too. You just need to set the env variable.. you can do that in Windows... – Patrick Desjardins Dec 04 '08 at 16:40
  • 7
    This solution won't work with windows (unless you call it from a unix style shell (eg cygwin)). The #! is handled by the shell, and windows does not support it. I believe in the example you googled it is being handled by the webserver, *not* from being launched in windows – Brian Dec 04 '08 at 17:00
  • Yeah, on Apache on Windows... the question request shell automaticly, you can do it with Cygwin, Apache, etc... – Patrick Desjardins Dec 04 '08 at 17:13
  • @Daok : I didn't downvote you. I'm grateful for help even an attempt at help. – minty Dec 04 '08 at 17:33
  • I do the same, just change the python.exe to python30.exe as mentioned. – monkut Dec 05 '08 at 01:18
  • 4
    One can use [pylauncher](https://bitbucket.org/vinay.sajip/pylauncher) to make something like this work (as described in [PEP 397 -- Python launcher for Windows](http://www.python.org/dev/peps/pep-0397/) in 2011. – martineau Jan 02 '13 at 05:28
14

Here is a neat and clean way to install Python2 & Python3 on windows.

https://datascience.com.co/how-to-install-python-2-7-and-3-6-in-windows-10-add-python-path-281e7eae62a

My case: I had to install Apache cassandra. I already had Python3 installed in my D: drive. With loads of development work under process i didn't wanted to mess my Python3 installation. And, i needed Python2 only for Apache cassandra.

So i took following steps:

  1. Downloaded & Installed Python2.
  2. Added Python2 entries to classpath (C:\Python27;C:\Python27\Scripts)
  3. Modified python.exe to python2.exe (as shown in image below)

enter image description here

  1. Now i am able to run both. For Python 2(python2 --version) & Python 3 (python --version). enter image description here

So, my Python3 installation remained intact.

Manjeet
  • 949
  • 14
  • 23
  • I can't believe you can simply rename `python.exe` to `python2.exe` and it still works in the command line. How odd. You don't even have to manually add `C:\Python27;C:\Python27\Scripts` to the `PATH` environment variable if you select to add it to the PATH in the installer. Python3 and Python2 both working on the same (`C:`) drive – velkoon Oct 26 '22 at 12:53
  • Should the pythonw.exe maybe be renamed to pythonw2.exe ? – mario.b Nov 13 '22 at 10:00
  • @mario.b - Yes you can rename it. it depends whether it's required for you or not. python.exe is associated with . py files and opens and runs in a terminal window. pythonw.exe is associated with . pyw files and does not open the terminal – Manjeet Dec 29 '22 at 14:23
  • @velkoon - You can see live working example in screenshot above. – Manjeet Dec 29 '22 at 14:23
9

I'm using 2.5, 2.6, and 3.0 from the shell with one line batch scripts of the form:

:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*

Name them pythonX.Y.bat and put them somewhere in your PATH. Copy the file for the preferred minor version (i.e. the latest) to pythonX.bat. (E.g. copy python2.6.bat python2.bat.) Then you can use python2 file.py from anywhere.

However, this doesn't help or even affect the Windows file association situation. For that you'll need a launcher program that reads the #! line, and then associate that with .py and .pyw files.

Community
  • 1
  • 1
  • See [my comment](http://stackoverflow.com/questions/341184/can-i-install-python-3-x-and-2-x-on-the-same-computer/341218#comment19538837_341218) about Patrick Desjardins's answer. – martineau Jan 02 '13 at 05:34
8

When you add both to environment variables there will a be a conflict because the two executable have the same name: python.exe.

Just rename one of them. In my case I renamed it to python3.exe.

So when I run python it will execute python.exe which is 2.7 and when I run python3 it will execute python3.exe which is 3.6

enter image description here

Charif DZ
  • 14,415
  • 3
  • 21
  • 40
  • Can you please elaborate the note part? When I rename python to python3, I can no longer run pip because it seems pip.exe, pip3.exe and pip3.7.exe all depends on python.exe. When I run pip, I get error => `Fatal error in launcher: Unable to create process using '"c:\users\\appdata\local\programs\python\python37-32\python.exe" "C:\Users\\AppData\Local\Programs\Python\Python37-32\Scripts\pip.exe" '`. – Mandar Sadye May 25 '19 at 13:10
  • I don't know I didn't have this problem – Charif DZ May 26 '19 at 08:44
  • @MandarSadye You can open pip.exe in notepad and search for python.exe.. Then replace python.exe with python3.exe – atg May 16 '20 at 12:17
7

Here you go...

winpylaunch.py

#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py <script.py> [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)

I've just knocked this up on reading this thread (because it's what I was needing too). I have Pythons 2.6.1 and 3.0.1 on both Ubuntu and Windows. If it doesn't work for you post fixes here.

  • 1
    Would be better to use `sys.exit(process.returncode)` to propagate the exit status of the invoked script to the caller. – martineau Jul 09 '12 at 15:48
5

Try using Anaconda.

Using the concept of Anaconda environments, let’s say you need Python 3 to learn programming, but you don’t want to wipe out your Python 2.7 environment by updating Python. You can create and activate a new environment named "snakes" (or whatever you want), and install the latest version of Python 3 as follows:

conda create --name snakes python=3

Its simpler than it sounds, take a look at the intro page here: Getting Started with Anaconda

And then to handle your specific problem of having version 2.x and 3.x running side by side, see:

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Cale Sweeney
  • 1,014
  • 1
  • 15
  • 37
4

As far as I know Python runs off of the commandline using the PATH variable as opposed to a registry setting.

So if you point to the correct version on your PATH you will use that. Remember to restart your command prompt to use the new PATH settings.

James McMahon
  • 48,506
  • 64
  • 207
  • 283
  • This is true if you type 'python myscript.py' you could then type C:\python30\python.exe instead but the command prompt does this automatically if you just type myscript.py and I don't want to break the 2.? scripts if I install 3.0 on the same machine. – minty Dec 04 '08 at 16:32
  • Oh and minty it might help to add the information in your first comment to the question, it was alittle a little nebulous. – James McMahon Dec 04 '08 at 16:50
3

Here is how to run Python 2 and 3 on the same machine

  1. install Python 2.x
  2. install Python 3.x
  3. Start Powershell
  4. Type Python -2 to launch Python 2.x
  5. Type Python -3 to launch Python 3.x

The Python Launcher for Windows was embedded into Python since Version 3.3, as promised in 2011 when the Stand alone first made its debut:

Python Launcher for Windows

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
rogerwamba
  • 72
  • 2
3

The Python installation normally associates .py, .pyw and .pyc files with the Python interpreter. So you can run a Python script either by double-clicking it in Explorer or by typing its name in a command-line window (so no need to type python scriptname.py, just scriptname.py will do).

If you want to manually change this association, you can edit these keys in the Windows registry:

HKEY_CLASSES_ROOT\Python.File\shell\open\command
HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command
HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command

Python Launcher

People have been working on a Python launcher for Windows: a lightweight program associated with .py and .pyw files which would look for a "shebang" line (similar to Linux et al) on the first line, and launch Python 2.x or 3.x as required. See "A Python Launcher for Windows" blog post for details.

Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
2

Easy-peasy ,after installing both the python versions add the paths to the environment variables ;seeenvironment variable settings. Then go to python 2 and python 3 folders and rename them to python2 and python3 respectively as shown here for python2 and here for python3. Now in cmd type python2 or python3 to use your required version see here.

1

I think there is an option to setup the windows file association for .py files in the installer. Uncheck it and you should be fine.

If not, you can easily re-associate .py files with the previous version. The simplest way is to right click on a .py file, select "open with" / "choose program". On the dialog that appears, select or browse to the version of python you want to use by default, and check the "always use this program to open this kind of file" checkbox.

Brian
  • 116,865
  • 28
  • 107
  • 112
  • This is what I did. I still have 2.5.2 and 3.0.1 on my Windows Vista box. I unchecked that in the install Wizard (might have said something like "Register with system" - I forget). Both work fine. For command line stuff, I put a py.bat file in my path that kicks off python 3 - nothing fancy, but does what I need. – Anon Jul 11 '09 at 23:46
1

Before I courageously installed both simultaneously, I had so many questions. If I give python will it go to py3 when i want py2? pip/virtualenv will happen under py2/3?

It seems to be very simple now.

Just blindly install both of them. Make sure you get the right type(x64/x32). While/after installing make sure you add to the path to your environment variables.

[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\PYTHONx", "USER")

Replace the x in the command above to set the path.

Then go to both the folders.

Navigate to

python3.6/Scripts/

and rename pip to pip3.

If pip3 already exists delete the pip. This will make sure that just pip will run under python2. You can verify by:

pip --version

In case you want to use pip with python3 then just use

pip3 install 

You can similarly do the same to python file and others.

Cheers!

1

You should make sure that the PATH environment variable doesn't contain both python.exe files ( add the one you're currently using to run scripts on a day to day basis ) , or do as Kniht suggested with the batch files . Aside from that , I don't see why not .

P.S : I have 2.6 installed as my "primary" python and 3.0 as my "play" python . The 2.6 is included in the PATH . Everything works fine .

Geo
  • 93,257
  • 117
  • 344
  • 520
1

I had the same problem where I wanted to use python3 for most work but IDA pro required python2. SO, here's what I did.

I first created 3 variables in the user environment variable as follows:

  1. PYTHON_ACTIVE : This is initially empty
  2. HOME_PYTHON27 : Has a path to a folder where Python 2 is installed. Eg. ";/scripts;"
  3. HOME_PYTHON38 : Similar to python 2, this variable contains a path to python 3 folders.

Now I added

%PYTHON_ACTIVE%

to PATH variable. So, basically saying that whatever this "PYTHON_ACTIVE" contains is the active python. We programmatically change the contains of "PYTHON_ACTIVE" to switch python version.

Here is the example script:

:: This batch file is used to switch between python 2 and 3.
@ECHO OFF

set /p choice= "Please enter '27' for python 2.7 , '38' for python 3.8 : "

IF %choice%==27 (
setx PYTHON_ACTIVE %HOME_PYTHON27%
)

IF %choice%==38 (
setx PYTHON_ACTIVE %HOME_PYTHON38%
)


PAUSE

This script takes python version as input and accordingly copies HOME_PYTHON27 or HOME_PYTHON38 to PYTHON_ACTIVE. Thus changing the global Python version.

Mandar Sadye
  • 689
  • 2
  • 9
  • 30
0

I would assume so, I have Python 2.4, 2.5 and 2.6 installed side-by-side on the same computer.

  • 1
    That my work for multiple versions of 2.x, but minty is asking if they can run 2.x alongside 3.x. There are some significant differences between the two. See: [Differences Between 2.x and 3.x](http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html) – Cale Sweeney May 16 '17 at 19:33
0

I am just starting out with python now. I'm reading Zed Shaw's book "Learn Python the Hard Way" which requires python version 2.x but am also taking a class that requires python 3.x

So here is what I did.

  1. Download python 2.7
  2. run power shell (should already be installed on windows)
  3. run python IN POWERSHELL (if it doesn't recognize then go to step 4)
  4. Only if powershell doesn't recognize python 2.7 type in the following:

"[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\PYTHON27", "USER")" (no outside quotes)

  1. Now type python and you should see it say python 2.7 blah blah blah

NOW for python 3.x

Simple, python 3.x download comes with python for windows app. SO simply pin the Python for Windows app to your task bar, or create shortcut to the desktop and you are done!

Open Python for Windows for 3.x

Open Powershell for python 2.x

I hope this helps!

0

Hmm..I did this right now by just downloading Python 3.6.5 for Windows at https://www.python.org/downloads/release/python-365/ and made sure that the launcher would be installed. Then, I followed the instructions for using python 2 and python 3. Restart the command prompt and then use py -2.7 to use Python 2 and py or py -3.6 to use Python 3. You can also use pip2 for Python 2's pip and pip for Python 3's pip.

FearlessFuture
  • 2,250
  • 4
  • 19
  • 25