ChatGPT解决这个技术问题 Extra ChatGPT

How to install both Python 2.x and Python 3.x in Windows

I do most of my programming in Python 3.x on Windows 7, but now I need to use the Python Imaging Library (PIL), ImageMagick, and wxPython, all of which require Python 2.x.

Can I have both Python 2.x and Python 3.x installed in Windows 7? When I run a script, how would I "choose" which version of Python should run it? Will the aforementioned programs be able to handle multiple versions of Python installed at once? I have searched for hours and hours for how to do this to no avail.

Thanks.

Yes, as @NickT says, that is easiest. Unfortunately, none of the answers below gives the simple, straightforward approach. (The ones mentioning version 3.3 come closest.) For a better answer, see the accepted one under the duplicate question: stackoverflow.com/a/17245543/1593924 In a nutshell, use the new "py" launcher, either with or without a -3 parameter. And consider adding shebangs (#!) to your scripts so you won't need a parameter.
Also, some have found the details I posted here helpful: stackoverflow.com/a/13533958/1593924

P
Pedro Vagner

I found that the formal way to do this is as follows:

Just install two (or more, using their installers) versions of Python on Windows 7 (for me work with 3.3 and 2.7).

Follow the instuctions below, changing the parameters for your needs.

Create the following environment variable (to default on double click):

Name:  PY_PYTHON
Value: 3

To launch a script in a particular interpreter, add the following shebang (beginning of script):

#! python2

To execute a script using a specific interpreter, use the following prompt command:

> py -2 MyScript.py

To launch a specific interpreter:

> py -2

To launch the default interpreter (defined by the PY_PYTHON variable):

> py

Resources

Documentation: Using Python on Windows

PEP 397 - Python launcher for Windows


In addition, to use easy_install or pip with both versions, change the path to include c:\python35;c:\python35\scripts;c:\python27\scripts;. This allows the use of pip2/pip3, and easy_install-2.7/easy_install-3.5 instead of pip and easy_install.
Won't work with various programs that don't ship their own Python and insist on using the system version, and don't respect the launcher.
No, don't add any Python to the path. Use py -m pip install .... Add switches after py to choose which Python to run pip under as above.
D
Davos

What I did was download both 2.7.6 and 3.3.4. Python 3.3.4 has the option to add the path to it in the environment variable so that was done. So basically I just manually added Python 2.7.6.

How to...

Start > in the search type in environment select "Edit environment variables to your account"1 Scroll down to Path, select path, click edit. Add C:\Python27; so you should have paths to both versions of Python there, but if you don't this you can easily edit it so that you do..... C:\Python27;C:\Python33; Navigate to the Python27 folder in C:\ and rename a copy of python.exe to python2.exe Navigate to the Python34 folder in C:\ and rename a copy of python.exe to python3.exe Test: open up commmand prompt and type python2 ....BOOM! Python 2.7.6. exit out. Test: open up commmand prompt and type python3 ....BOOM! Python 3.4.3. exit out.

Note: (so as not to break pip commands in step 4 and 5, keep copy of python.exe in the same directory as the renamed file)


This will break both pip2 and pip3! Don't replace python, copy it and rename the second file to python2/python3.
just a word of caution, I was unable to uninstall python until I reverted the names back. My advise is to make copies and rename those but leave the original python.exe files alone **as also mentioned by bruno .
Could you process brunofitas feedback in the answer?
Yeah, I just renamed the python.exe in the Python3 installation to python3.exe. I think this automatically happens with python in a *nix environment. Of course there are not .exe files but there is a python2 and python3 program. :)
If you have a library/plugin that depends on either version of python, make sure that version comes first in your PATH environment variable
C
Community

I have multiple versions in windows. I just change the exe name of the version I'm not defaulting to.

python.exe --> python26.exe pythonw.exe --> pythonw26.exe

As for package installers, most exe installers allow you to choose the python install to add the package too. For manual installation check out the --prefix option to define where the package should be installed:

http://docs.python.org/install/index.html#alternate-installation-windows-the-prefix-scheme


This worked. Before making this change, when I tried to run a program in Python 2.7, it still ran in Python 3.1 (Perhaps because Windows 7 can't handle two different programs with the same name). After renaming the exe's as shown, everything went well.
This worked for me also. I had a conflict between Python 2.7 and Python 3.2. As I had installed Python 3.2 first (and set appropriately as an environmental variable), I couldn't access Python 2.7 in the prompt (instead, I got Python 3.2). I wanted to use as default Python 2.7, therefore, I changed python.exe to python3.2.exe and kept python.exe of Python 2.7 unchanged. That did the trick.
I haven't installed a new python3 on windows in awhile, but the version I have includes pip as pip3 and pip3.4. (it also includes it as pip, but I would think deleting that should resolve any conflicts with python2). Actually I don't have C:\Python34\Scripts in my Path, so I haven't had any pip related problems...
p
philshem

If you use Anaconda Python, you can easily install various environments.

Say you had Anaconda Python 2.7 installed and you wanted a python 3.4 environment:

conda create -n py34 python=3.4 anaconda

Then to activate the environment:

activate py34

And to deactive:

deactivate py34

(With Linux, you should use source activate py34.)

Links:

Download Anaconda Python

Instructions for environments


IMO conda is by far the best way to mess with different Python installations. I messed up so many things before moving to Anaconda.
K
KeyC0de

To install and run any version of Python in the same system follow my guide below.

For example say you want to install Python 2.x and Python 3.x on the same Windows system.

Install both of their binary releases anywhere you want. When prompted do not register their file extensions and do not add them automatically to the PATH environment variable Running simply the command python the executable that is first met in PATH will be chosen for launch. In other words, add the Python directories manually. The one you add first will be selected when you type python. Consecutive python programs (increasing order that their directories are placed in PATH) will be chosen like so: py -2 for the second python py -3 for the third python etc.. No matter the order of "pythons" you can: run Python 2.x scripts using the command: py -2 (Python 3.x functionality) (ie. the first Python 2.x installation program found in your PATH will be selected) run Python 3.x scripts using the command: or py -3 (ie. the first Python 3.x installation program found in your PATH will be selected)

In my example I have Python 2.7.14 installed first and Python 3.5.3. This is how my PATH variable starts with:

PATH=C:\Program Files\Microsoft MPI\Bin\;C:\Python27;C:\Program Files\Python_3.6\Scripts\;C:\Program Files\Python_3.6\;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Intel\Shared

...

Note that Python 2.7 is first and Python 3.5 second.

So running python command will launch python 2.7 (if Python 3.5 the same command would launch Python 3.5).

Running py -2 launches Python 2.7 (because it happens that the second Python is Python 3.5 which is incompatible with py -2). Running py -3 launches Python 3.5 (because it's Python 3.x)

If you had another python later in your path you would launch like so: py -4. This may change if/when Python version 4 is released.

Now py -4 or py -5 etc. on my system outputs: Requested Python version (4) not installed or Requested Python version (5) not installed etc.

Hopefully this is clear enough.


How could you specify a pip install in one of the Python dists?
For the first python found simply type pip. For any other python installed you have to manually specify the path, ie. C:\Python27\Scripts\pip.py [parameters]. (Of course, as a shortcut, you can put any exe you want in an environment variable.)
@Nik-Lz what "py -2" command is it, that you are referring to? I don't seem to have these on my windows 7.
This is exactly how should python be used on windows in case of multiple versions.
@RamShankarKumar You append to whatever python command you use (as described in the answer) the -m pip install nameOfModule. For Windows I use pip-Win to make my life easier.
T
Tonechas

Here's what you can do:

Install cmder. Open and use Cmder as you would with you cmd terminal. Use the command alias to create command aliases.

I did the following:

alias python2 = c:\python27\python.exe
alias python3 = c:\python34\python.exe

And that's it! ;-)


set-alias python2 c:\python27\python.exe
Use set-alias as example show if using Powershell. In that case there would be no need to install Cmder.
@ProphetDaniel This should be posted as a separate answer. It totally answers the original question.
E
Efren

Starting version 3.3 Windows version has Python launcher, please take a look at section 3.4. Python Launcher for Windows


S
Skylion

I actually just thought of an interesting solution. While Windows will not allow you to easily alias programs, you can instead create renamed batch files that will call the current program.

Instead of renaming the executable which will break a lot of thing including pip, create the file python2.bat in the same directory as the python2.exe. Then add the following line:

%~dp0python %*

What does this archaic syntax mean? Well, it's a batch script, (Windows version of bash). %~dp0 gets the current directory and %* will just pass all the arguments to python that were passed to the script.

Repeat for python3.bat

You can also do the same for pip and other utilities, just replace the word python in the file with pip or whathever the filename. The alias will be whatever the file is named.

Best of all, when added to the PATH, Windows ignores the extension so running

python3

Will launch the python3 version and and the command python2 will launch the python2 version.

BTW, this is the same technique Spyder uses to add itself to the path on Windows. :)


Awesome solution without modifying existing python files.
l
li.davidm

You can install multiple versions of Python one machine, and during setup, you can choose to have one of them associate itself with Python file extensions. If you install modules, there will be different setup packages for different versions, or you can choose which version you want to target. Since they generally install themselves into the site-packages directory of the interpreter version, there shouldn't be any conflicts (but I haven't tested this). To choose which version of python, you would have to manually specify the path to the interpreter if it is not the default one. As far as I know, they would share the same PATH and PYTHONPATH variables, which may be a problem.

Note: I run Windows XP. I have no idea if any of this changes for other versions, but I don't see any reason that it would.


I have python 2.5, 2.6 and 3.1 all installed simultaneously, It never even occured to me that i might have to do something harder than just run all of the installers for each version I need.
B
Bryan Kimani

What I have done on my own windows computer where I have Python 2.7 and Python 3.4 installed is I wrote a simple .bat file in the same directory as my Python.exe files. They look something like,

cmd /k "c:\python27\python.exe" %*

The %* allows you to add arguments (Python files) afterwards. I believe /k keeps the prompt open after it finishes running the script. Then I save that as python27.bat Then I go to my Python 3 directory and make a bat file there. Now in my command line I can write

Python27 helloworld.py

Or

Python34 helloworld.py

And they will run in their respective versions of Python. Make sure that c:\python27 and c:\python34 are in your environment variables.

I got my answer from here


M
Moiz Kapadia

I did this in three steps by following the instructions here: This is all taken directly from here: http://ipython.readthedocs.io/en/stable/install/kernel_install.html. I'm currently running Python 2.x on Windows 8 and have Anaconda 4.2.13 installed.

1) First install the latest version of python:

conda create -n python3 python=3 ipykernel

2) Next activate python3

activate python3

3) Install the kernel:

python -m ipykernel install --user

If you have Python 3 installed and want to install 2, switch the 2 and the 3 above. When you open a new notebook, you can now choose between Python 2 or 3.


S
Spitfire19

Check your system environment variables after installing Python, python 3's directories should be first in your PATH variable, then python 2.

Whichever path variable matches first is the one Windows uses.

As always py -2 will launch python2 in this scenario.


T
Tonechas

I have encountered that problem myself and I made my launchers in a .bat so you could choose the version you want to launch.

The only problem is your .py must be in the python folder, but anyway here is the code:

For Python2

@echo off
title Python2 Launcher by KinDa
cls
echo Type the exact version of Python you use (eg. 23, 24, 25, 26)
set/p version=
cls
echo Type the file you want to launch without .py (eg. hello world, calculator)
set/p launch=
path = %PATH%;C:\Python%version%
cd C:\Python%version%
python %launch%.py
pause

For Python3

@echo off
title Python3 Launcher by KinDa
cls
echo Type the exact version of Python you use (eg. 31, 32, 33, 34)
set/p version=
cls
echo Type the file you want to launch without .py (eg. hello world, calculator)
set/p launch=
cls
set path = %PATH%:C:\Python%version%
cd C:\Python%version%
python %launch%.py
pause

Save them as .bat and follow the instructions inside.


a
abudhabikid

Install the one you use most (3.3 in my case) over the top of the other. That'll force IDLE to use the one you want.

Alternatively (from the python3.3 README):

Installing multiple versions

On Unix and Mac systems if you intend to install multiple versions of Python using the same installation prefix (--prefix argument to the configure script) you must take care that your primary python executable is not overwritten by the installation of a different version. All files and directories installed using "make altinstall" contain the major and minor version and can thus live side-by-side. "make install" also creates ${prefix}/bin/python3 which refers to ${prefix}/bin/pythonX.Y. If you intend to install multiple versions using the same prefix you must decide which version (if any) is your "primary" version. Install that version using "make install". Install all other versions using "make altinstall".

For example, if you want to install Python 2.6, 2.7 and 3.3 with 2.7 being the primary version, you would execute "make install" in your 2.7 build directory and "make altinstall" in the others.


w
wheya

I just had to install them. Then I used the free (and portable) soft at http://defaultprogramseditor.com/ under "File type settings"/"Context menu"/search:"py", chose .py file and added an 'open' command for the 2 IDLE by copying the existant command named 'open with IDLE, changing names to IDLE 3.4.1/2.7.8, and remplacing the files numbers of their respective versions in the program path. Now I have just to right click the .py file and chose which IDLE I want to use. Can do the same with direct interpreters if you prefer.


w
walthor

I use a simple solution to switch from a version to another version of python, you can install all version you want. All you have to do is creating some variable environment. In my case, I have installed python 2.7 and python 3.8.1, so I have created this environment variables:

PYTHON_HOME_2.7= PYTHON_HOME_3.8.1= PYTHON_HOME=%PYTHON_HOME_2.7%

then in my PATH environment variable I put only %PYTHON_HOME% and %PYTHON_HOME%\Scripts. In the example above I'm using the version 2.7, when I want to switch to the other version I have only to set the PYTHON_HOME=%PYTHON_HOME_3.8.1%. I use this method to switch quickly from a version to another also for JAVA, MAVEN, GRADLE,ANT, and so on.


A
Alexandre Bell

Only Works if your running your code in your Python IDE

I have both Python 2.7 and Python 3.3 installed on my windows operating system. If I try to launch a file, it will usually open up on the python 2.7 IDE. How I solved this issue, was when I choose to run my code on python 3.3, I open up python 3.3 IDLE(Python GUI), select file, open my file with the IDLE and save it. Then when I run my code, it runs to the IDLE that I currently opened it with. It works vice versa with 2.7.


S
Sergio Ayestarán

I have installed both python 2.7.13 and python 3.6.1 on windows 10pro and I was getting the same "Fatal error" when I tried pip2 or pip3.

What I did to correct this was to go to the location of python.exe for python 2 and python 3 files and create a copy of each, I then renamed each copy to python2.exe and python3.exe depending on the python version in the installation folder. I therefore had in each python installation folder both a python.exe file and a python2.exe or python3.exe depending on the python version.

This resolved my problem when I typed either pip2 or pip3.


A
Alex P.

If you can't get anything else to work, open an interpreter in whichever version you choose (I prefer using iPython) and:

import subprocess

subprocess.call('python script.py -flags')

This uses whichever python version you are currently operating under. Works fine for a single script, but will quickly get out of hand if there are lots of scripts you run, in which case you can always make a batch file with all of these calls inside. Not the most elegant answer, but it works.

Is there a way to make aliases for different python version a la Linux?