Python 2 and Python 3 on the same computer¶
Installation¶
Base Python¶
Linux¶
Just use your package manager to install both python2 and python3 (if it's not already installed). For instance on Debian-like:
$ sudo apt install python2 python3
Windows using chocolatey¶
Important
at the time of writing this documentation, installing python3 will install a newer version than the one in PyQt5 dependencies, leaving you with multiple python3 on your system
I do not know what it implies, but let's assume it should do the things right. You will need a Terminal with elevated privileges:
- [Windows key], then cmd then [ENTER]
- RMB on the taskbar icon of the terminal,
- RMB on the Command Terminal menu item
- select the start with elevated privileges

Type the following commands:
$> choco install -y python2
$> choco install -y python3
Windows¶
On windows things get a little bit harder, go to https://python.org and get both the latest python 2.7 and python 3.x installers (executable installers).
Install python2 letting installation go to the default installation directory (C:\Python27). There is no need to add python in the PATH.
Now, install python3 using administrator Privileges (RMB on installer to get the option).
Select customize installation (Adding python in PATH is not mandatory):

Keep all the optional features:

In Advanced options, check (at least):
- install for all users
- Associate files with Python
- Add Python to environment variables

Note
In the last panel, there is an option for removing the 260 char path length limit.
I don't know what it does, so I not advice you to go through it, but it's definitively something to investigate in the future.

PyQt5¶
Important
PyQt5 is in GPL & commercial licensing: I.E. we can code helper scripts internally, but we will need a commercial license to release stuff to clients
interesting comparison between PyQt5 and QtSide2
Linux¶
Just use your package manager for handling PyQt5 installation (available for both Python2 and Python3). For instance on Debian-like:
$ sudo apt install python3-pyqt5 python-pyqt5
Windows with chocolatey¶
Important
as said above, at the time of writing this documentation, PyQt5 installation using chocolatey will trigger an installation of python3.4 aside your python3.7 installed by chocolatey, this said, it's possible to have multiple python but will put you in trouble when you need specific packages.
Note
you can also install PyQt5 using the workflow described below, using PIP
To install PyQt5 with chocolatey, simply type those commands in an elevated privileges terminal:
$> choco install -y pyqt5
Windows¶
PyQt5 is only available (in binary form) for Python3 on Windows. It is possible to compile your own version of PyQt5 for Python2.X but it's out of the scope of this page.
To install PyQt5, we will use the Python package manager (PIP). As we installed Python3 for all users, PIP will try to install PyQt5 for all users too, hence it needs a terminal with elevated privileges in place.
To start one:
- [Windows key], then cmd then [ENTER]
- RMB on the taskbar icon of the terminal,
- RMB on the Command Terminal menu item
- select the start with elevated privileges

In this terminal, verify that you can run python code:
C:\WINDOWS\system32>python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
This is OK, we can run python3 code (for python2 see the section about running python2 vs python3 code)
Now, to perform the PyQt5 installation, simply type:
$> python -m pip install pyqt5
Collecting pyqt5
Downloading https://files.pythonhosted.org/packages/3b/d3/76670a331935f58f9a2ebd53c6e9b670bbf15c458fa6993500af5d323160/PyQt5-5.13.0-5.13.0-cp35.cp36.cp37.cp38-none-win_amd64.whl (49.7MB)
|████████████████████████████████| 49.7MB 6.8MB/s
Collecting PyQt5_sip<13,>=4.19.14 (from pyqt5)
Downloading https://files.pythonhosted.org/packages/cf/c8/1e9eda4ba09a84fc528d4c87001de2d7a8cbbe04c2a834af3eb81a0ecd88/PyQt5_sip-4.19.18-cp37-none-win_amd64.whl (51kB)
|████████████████████████████████| 61kB ...
Installing collected packages: PyQt5-sip, pyqt5
Successfully installed PyQt5-sip-4.19.18 pyqt5-5.13.0
To test it simply start python in your terminal (see above) and try to run this command: import PyQt5 . If python is not complaining that this module is unknown, then your installation went OK.
One can see Video demonstration.
running python2 vs python3 code¶
Linux¶
On Linux, Python executables are versioned, hence it's easy to select on which python you want to run your script/interpreter.
$ python2 my_python_2_script.py
$ python3 my_python_3_script.py
If the script has the executable flag set, then you can run your script directly without choosing the python version on the command line:
$> my_python_script.py
The python interpreter has to be set by adding a shebang at the start of your script. This way, the script "embed" what interpreter to use (some kind of best solution).
Example shebangs:
- python2: #!/usr/bin/env python2
- python3: #!/usr/bin/env python3
Windows¶
On windows, since Python 3.3 which embed PEP 397 it becomes easier to handle multiple Python interpreters on windows.
In the python 3.7 installer, the python-launcher is automatically installed, bringing a new way to launch python script using the interpreter you want. You only have to use py.exe and pyw.exe instead of python.exe and pythonw.exe. This will parse the python script for guessing what interpreter to use (for instance it can use shebang in top of the script), but you can also force a specific version using the terminal. For instance:
$ py my_python_script_to_run_with_default_interpreter.py
$ py -2 my_python_2_script.py
$ py -3 my_python_3_script.py
$ py -3.4 my_python_3.4_script.py
$ py -3.7 my_python_3.7_script.py
Using this tool you can also list Python interpreters it is aware of by calling:
C:\WINDOWS\system32>py --list-paths
Installed Pythons found by py Launcher for Windows
-3.7-64 C:\Python37\python.exe *
-2.7-64 C:\Python27\python.exe
This said, if you associate your *.py files to be launched by the python-launcher (C:\Windows\py.exe) then, shebang in your script will be enough for "python-launcher" to know which python to use. Almost like what is it's done on Linux.