in Lifehacki

Python 3.6 and 2.7 side by side

In order to use two versions of Python on one Windows 10 machine while keeping the 3.6 as the default environment we can use a simple trick. The final solution gives us following effect:

>  python --version
Python 3.6.1
>  py2
>  python --version
Python 2.7.13
>  py3
>  python --version
Python 3.6.1

The procedure is fairly easy and involves two simple one-line scripts. During Python 3.6 installation you should add the executable folder to environmental variables. Note the selected installation path as we’ll use it later.

Python 3.6 advanced installation settings

Check if everything works and install Python 2.7 but now without adding it to environmental Path variable.

I changed the default installation path to correspond with Python 3.6 path to C:\Program Files\Python27.

At this point the Python 2.7 installation is silent and we cannot run it straight from the command line. The simplest solution I can think of is modifying the Path variable temporarily by adding respective installation path at the beginning of the $env:Path.

So I created two simple one-liners that can be called to switch python versions. The first one is called py2.ps1 (default PowerShell script extension).

$env:Path = "C:\Program Files\Python27;C:\Program Files\Python27\Tools\Scripts" + $env:Path

The second is obviously py3.ps1:

$env:Path = "C:\Program Files\Python36;C:\Program Files\Python36\Scripts" + $env:Path

The $env:Path variable resets with each PowerShell restart.

To simplify the script triggering I added my scripts folder to Windows system environmental Path variables. I keep them on Dropbox to synchronize between machines.