beawolf - Fotolia

Resolve issues between Python and Linux with virtualenv

While Linux OSes favor Python 2, Python 3 is the widely preferred version. Install virtualenv to isolate Python 2 and 3 and prevent IT issues.

Ubuntu, Debian and other versions of Linux still ship with Python 2 installed, despite Python 3's decade on the scene.

Python 3 debuted in 2008. Python 2's final version, 2.7, came out in 2010. Python 2 will reach end of life in 2020, five years later than originally planned. Python 2 users argued that they could not, or did not want to, upgrade earlier than this 2020 timeframe.

Developers and system administrators need to use Python and Linux together while these two versions of Python are available. The best path for many IT organizations is to run version 2.x and 3.x at the same time on a system.

Python versions

Python 2 and 3 are almost the same. The most famous deprecated feature of Python 2 is a requirement to put parentheses () around the print() statement. Because new development is focused on Python 3, users who cling to Python 2 could miss new packages. The scenario is not always either/or. For example, the cloud orchestration system OpenStack, which is written in Python, uses either version. The open source TensorFlow machine learning library is available in both as well.

Linux and Python run into messy situations due to the two versions. To illustrate, the request which python on a new Ubuntu server yields the response:

/usr/bin/python

The Ubuntu server runs Python 2. An admin can install Python 3 in addition and use Python 3. To run a Linux system with Python 3, always include python3 instead of python, or create an alias that points to python3 or a soft link that enables the user simply to type python.

When users install Python packages, they go into two folders, depending on whether they use the package manager pip2 or pip3:

/usr/lib/python3.4

/usr/lib/python2.7

Put the wrong version of Python into the wrong folder on the Linux box, and you can end up with an unrecoverable situation (see Figure 1).

Python on Linux problems
Figure 1. An incorrect installation of Python on Linux leads to an impossible situation with broken packages.

An admin might drill down and try to install the Depends on packages in an attempt to fix the situation, but that can create a further mess (see Figure 2). Admins typically fix broken dependencies via sudo apt-get install -f, but this method is usually ineffective with Python and Linux.

Python package problems on Linux
Figure 2. The problem of Linux and Python versions deepens, even with the use of Depends -- and so forth, ad infinitum.

To resolve Python and Linux problems, install the virtualenv Python environment isolation tool. Virtualenv creates multiple Python environments for multiple users. It works by installing Python 2 and Python 3 binaries in the target folder, which is preferably the user's home folder. Then, it installs Python packages in (target folder)/lib. It also can share systemwide packages.

Install virtualenv

To start working with virtualenv, enter sudo apt-get install virtualenv. Or try other variations on the install process.

Install pip3:

sudo apt-get install python3-pip

It installs systemwide. Virtualenv installs another pip3 in the (target folder). Change to the home directory, then create a python2 virtual environment, in this case (target folder):

cd virtualenv (target folder)

Bring up this python2 environment via the commands:

source (target folder)/bin/activate

Check that the Linux system is now running Python in (target folder)/bin/ and not the system Python:

which python

/home/(your id)/(target folder)/bin/python

Set up the python3 virtualenv environment:

virtualenv -p python3 (target folder)

Then, type python3 to run Python 3.

Check that pip3 is installed:

which pip3

/home/(your id)/(target folder)/bin/pip3

Install a package, and check that it is installed in (target folder)/lib:

pip3 install requests

 

ls (target folder)/lib/python3.5/site-packages/requests

Switch between Python versions

If a server already has Python 3 but a given task or program requires Python 2, use this command:

virtualenv --python=/usr/bin/python (target directory)

source (target directory)/bin/activate

python

Dig Deeper on Systems automation and orchestration