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.
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):
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):
Documentation: Using Python on Windows
PEP 397 – Python launcher for Windows
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.
Start > in the search type in environment select “Edit environment variables to your account”1
Scroll down to Path, select path, click edit.
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)
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:
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:
And to deactive:
(With Linux, you should use
source activate py34.)
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
pythonthe 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
- py -3 for the third
- py -2 for the second
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 FilesMicrosoft MPIBin;C:Python27;C:Program FilesPython_3.6Scripts;C:Program FilesPython_3.6;C:ProgramDataOracleJavajavapath;C:Program Files (x86)Common FilesIntelShared
Note that Python 2.7 is first and Python 3.5 second.
- So running
pythoncommand will launch python 2.7 (if Python 3.5 the same command would launch Python 3.5).
py -2launches Python 2.7 (because it happens that the second Python is Python 3.5 which is incompatible with
py -3launches 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.
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.
Here’s what you can do:
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:python27python.exe alias python3 = c:python34python.exe
And that’s it! 😉
Starting version 3.3 Windows version has Python launcher, please take a look at section 3.4. Python Launcher for Windows
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:
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
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. 🙂