What is Python? Python is one of the most used programming languages both by the learners and experienced developers. It is easy to understand and uses a more readable language than any other programming language, making it suitable for beginners and makes it easy to develop web applications. Python has also been used widely as a scripting language, enabling automated processes in Linux systems. In technical terms, python is defined as an object-oriented, high-level programming language with integrated dynamic semantics primarily for web and app development.

Python 2 vs Python 3

To be able to run Python programs, one is required to have installed Python interpreter in their systems. Python 2 was released in the year 2000, and later version 2.7 in 2010. In 2008, Python3 was released and it started gaining more popularity over python2. Currently, Python 3 is the default Python interpreter. Ubuntu 20.04 and other versions of Debian Linux ship with Python 3 pre-installed. That being said, Python 2 is legacy while Python3 is the future and most developers are currently writing libraries to be used strictly with Python3. However, some companies still use Python2 for legacy reasons.

How then do I install Python2 on Linux Mint? In this guide, we are going to look at how to install Python2 with virtualenv on Linux Mint 20.

Install Python 2 on Linux Mint 20

Before any installations, first update and upgrade your system apt. Run the commands below:

sudo apt-get update
sudo apt-get upgrade

In this guide, we are going to be using PIP (a Python Package Manager) to install various Python packages. We, therefore, need to install pip for Python2. We require Universe repository and get-pip.py script for installing Python 2 PIP tool.

sudo add-apt-repository universe
sudo apt update

Now, let’s install python2 on Linux Mint 20

sudo apt install python2

Next, we are going to download get-pip.py script for installing Python 2 PIP tool using the below command.

curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py

Install Python 2 pip tool

Run the command as shown below to install python2 pip tool

sudo python2 get-pip.py

You should seen an output as below when pip is successfully installed.

You can verify PIP installation by checking on the installed version.

$ pip -V

Output

Install Python2 Virtualenv

What is Python virtual environment? Virtualenv is a tool for creating Python isolated virtual environments. It enables managing of python projects packages to avoid installing them globally which could tamper with system tools or other projects. Having installed pip2, run the below command to install python2 virtualenv

sudo pip install virtualenv

Python2 Virtualenv basic usage

Having installed virtualenv, we need to create a directory for virtual environment.

mkdir ~/.virtualenvs

Let’s also install virtual environment wrapper for managing virtual environments . Run the below command.

sudo pip install virtualenvwrapper

Now change to the virtualenv created directory

cd ~/.virtualenvs

In the directory, create a virtual environment for your project. You can do this by specifying the Python interpreter you wish to use as shown.

$ virtualenv -p /usr/bin/python2.7 venv

Or you can change the global Python interpreter by running the below command.

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7

Next, let’s activate out virtualenv to start using it.

source venv/bin/activate

You will notice that the name of the virtual environment now appears of the left part of your command line, showing that you are working on an active virtualenv.

You can go ahead to install your required packages for your project using pip command. As an example, let’s install ‘requests module’. It is a python module that enables sending http requests using python.

$ pip install requests

Once done with working in the virtual environment, you can deactivate it by running the below command:

$ deactivate

If using virtualenvwrapper, run the below commands. As explained before, virtualenvwrapper makes working with virtual environment more pleasant. It also puts all your virtual environments in one place.

$ pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ source /usr/local/bin/virtualenvwrapper.sh

You should be able to see the virtual activating giving an output as below:

To create a virtual environment, run the command as shown:

mkvirtualenv project_folder

To work on the virtual environment, use the commend as shown below:

workon project_folder

Your terminal should appear as below:

You can deactivate the virtualenv the same way using deactivate command.

deactivate

To detele a virtualenv, use the rm command.

rmvirtualenv venv

Enjoy using Python 2 with virtualenv on Linux Mint 20. Do not forget to check more interesting Linux guides below!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

eight + 5 =