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
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
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
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
1 2 3 4
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 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
If it returns a version number, everything is fine. Otherwise, install it with the command
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
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
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.
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
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.
Because your Linux environment is now configured to use the virtual environment, you can just use
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.