Skip to content

Python

Generally speaking, it is best to use the most recent version of Python. These notes refer to Python 3.6 as an example. Check that it is installed by inspecting the contents of the directory /usr/bin using the command

1
ls -l /usr/bin/python*

You should see a response similar to the one below which shows that there are two versions of Python currently installed, 2.7 and 3.6.

1
2
3
4
5
lrwxrwxrwx 1 root root       9 Jan  9 15:21 /usr/bin/python -> python2.7
lrwxrwxrwx 1 root root       9 Apr 16  2018 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 3637096 Nov  7 10:07 /usr/bin/python2.7
lrwxrwxrwx 1 root root       9 Oct 25  2018 /usr/bin/python3 -> python3.6
-rwxr-xr-x 2 root root 4526456 Nov  7 10:44 /usr/bin/python3.6

The listing entries which contain an arrow symbol (->) are symbolic links. These act as pointers to other files. In the example above, we can see that the link /usr/bin/python points to the file /usr/bin/python2.7. This means that by default when a Python command is executed, it will use the python2.7 interpreter.

To make Python 3.6 the default, execute the commands shown below. The first pair removes the existing symbolic links, and the second pair recreates them pointing to python3.6.

1
2
3
4
sudo rm /usr/bin/python
sudo rm /usr/bin/python3
sudo ln -s /usr/bin/python3.6 /usr/bin/python
sudo ln -s /usr/bin/python3.6 /usr/bin/python3

In what follows, we need to make sure that we use the correct versions of the Python tools including pip, the Python package manager, and virtualenv, the tool used to create a virtual environment.

pip

Pip is a package manager that can be used to add, remove and update the packages that are included in your virtual environment.

If pip is already installed it is probably the version used with Python 2.7. To check whether pip3 is installed, use the command

1
pip3 --version

If it returns a version number, everything is fine. Otherwise, install it with the command

1
2
sudo apt update
sudo apt-get -y install python3-pip

Virtual environment

Python itself is simply an executable file somewhere in the Linux file system. The Python environment consists of this file plus a set of packages for different purposes. There can be several different versions of Python available on the same server. Environment variables in the Linux shell determine which one is used. Usually, there is a default Python executable for the whole system. However, it is possible to create a separate Python environment exclusively for a specific application. This is so that it can be configured exactly as required without affecting the default installation. This separate copy is called a virtual environment. It can be activated so that any Python commands are executed by the virtual environment rather than the default installation. Activation is really just a question of setting the appropriate environment variables, but there is a simplified method for this.

First, install the utility for creating Python virtual environments with

1
sudo apt-get install python3.6-venv

Next, we need to create some of the directory structure shown in the overview section. The first one is the shared location for application environments. This is required so that multiple users can access the contents and the contents are safe in the case where a user account is removed or altered in some way. The second directory is the one specifically intended to contain this particular application.

1
2
3
sudo mkdir /usr/local/env
sudo mkdir /usr/local/env/myApp
sudo chmod o+w /usr/local/env/myApp

The third command above sets the application directory to be writable by any user. This is fine during development (as long as the developers are careful), but it would be a security risk in a production environment.

To actually create the virtual environment, use the following commands the first of which changes to the directory you just created.

1
2
cd /usr/local/env/myApp
python -m venv venv

To confirm that the environment has been created, you can check the contents of the directory you are in.

To activate the virtual environment, use the command

1
. /usr/local/env/myApp/venv/bin/activate

You will notice that doing this alters the appearance of the Linux prompt. You should see the name of the virtual environment in brackets at the start. This acts as a reminder that a particular virtual environment is active.

You can see some of the effects of an active virtual environment by running the commands below which tell you which version of a given command will be run if you execute it.

1
2
which python
which pip

Because your Linux environment is now configured to use the virtual environment, you can just use the commands python and pip without worrying about getting the correct version. Also, any packages you install with pip will become part of the virtual environment rather than the default system-wide Python environment.