You are on page 1of 3

HOWTO use Python in a Virtual Environment on Linux

by ullix, September 4, 2020

When you install several Python3 versions – you shouldn't be using Python2 anymore, but this
HOWTO is applicable to it as well – and perhaps multiple modules for each, then chaos is almost
guaranteed and at the end none of the installations may work anymore due to various conflicts!
Whenever you want to install more than a single Python3 version on your computer, you should put
them in a Python-specific Virtual Environment (VE). Such a VE is different from “virtual
machines” of whole computing environments, and fortunately a whole lot simpler!
For the purpose of testing of GeigerLog I am currently running Python in versions 3.5, 3.6, 3.7, 3.8,
3.9. Each version of Py is allowed to load exactly the modules needed by it, and in exactly the
required module version.
But even if I had only a single Py version, but two or more Python programs, which each required a
module in a specific version, different from what the others needed, I could solve this problem with
a Python Virtual environment.
Using GeigerLog as example, I’ll show what the steps are.

Getting the Python Versions


For Ubuntu you can easily get various Python versions from a Launchpad.ppa:
https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa
For other distributions you can get them from their repositories, or from the GitHub archive:
https://github.com/deadsnakes
What you need to install (with X=5, 6, 7, 8, 9, …) is:
python3.X
python3.X-dev
python3.X-minimal
python3.X-distutils
python3.X-venv
Dependencies will be installed automatically.

Creating the Virtual Installations


At a location of my choice I create one folder with a name of my choice for each Py version which I
want to virtualize.

HOWTO use Python in a Virtual Environment on Linux Page 1 of 3


Let’s say I have a folder /home/user/GL which holds all my GeigerLog folders and files. Using the
terminal I will create folders named vglX (with X as above) as my virtual Py folders.
user@mymachine:~$ mkdir /home/user/GL/vgl3X

The folder with the GeigerLog code will not be changed, moved, or copied. It will remain a single
installation, and all virtual Pythons will access it. Of course, nothing prevents me from making
copies of GeigerLog and perhaps modify them specifically for each virtual installation, but it is not
required!
The I change into folder GL:
user@mymachine:~$ cd /home/user/GL

and execute – NOT as sudo! – :


user@mymachine:~/GL$ python3.X -m venv vgl3X

Starting a Virtual Environment


Then I start Py3.X in this VE with:
user@mymachine:~/GL$ source vgl3X/bin/activate

Your terminal then shows code at the beginning of each line – here highlighted in red – that you are
working in a virtual Python environment:
(vgl3X) user@mymachine:~/GL$

All further Python and Pip commands will now go ONLY into this VE.

Attention: from now on execute Pip only as a Python module, NEVER


directly as commands pip or pip3, and also NEVER use sudo or
sudo -H, because that would circumvent the VE!

Installing the modules with Pip


At this stage the “.X” at the Python commands is no longer necessary, because the Python version is
forced by the selected VE. Even the “3” is no longer needed for the same reason, but I continue to
use it for clarity and compatibility with the default installation.
First you should update Pip, because some functions need the lastest Pip version:
(vgl3X) user@mymachine:~/GL$ python3 -m pip install -U pip

The “-U” is short for “--upgrade”; if not given the existing Pip would be kept, even if a newer one
were available.

HOWTO use Python in a Virtual Environment on Linux Page 2 of 3


Now you can install the other modules needed, like for GeigerLog:
(vgl3X) user@mymachine:~/GL$ python3 -m pip install -U numpy
scipy matplotlib pyserial

and all others entered as a single line with a space between all module names.

Starting GeigerLog in a Virtual Environment


To start GeigerLog within any VE you enter:
(vgl3X) user@mymachine:~/GL$ python3 ./geigerlog/geigerlog

Sometimes you might prefer to use the full path, like:


(vgl3X) user@mymachine:~/GL$ python3 /path/to/geigerlog

Ending the Virtual Environment


To end working in a VE in order work without one or to switch to a different one, you enter:
(vgl3X) user@mymachine:~/GL$ deactivate

and you will get a terminal line without the “(vgl3X)” at the beginning:
user@mymachine:~/GL$

When you now use Python you will be using your default installation.

Working Simultaneously in Multiple Virtual Environments


This can be done easily by simply opening multiple terminals and starting a VE in each.
But note that when GeigerLog is installed only once, and you call this single installation in each
VE, they will all access the same config file of GeigerLog, same log file, and same data file if it has
the same name! This will likely have an undesired outcome.
In such a case, it will be beneficial to make an individual GeigerLog installation for each VE.
If you start a VE and end it before starting the next, there is no need for multiple GeigerLog
installations.

HOWTO use Python in a Virtual Environment on Linux Page 3 of 3

You might also like