In Focus

Steps To Set Up A Virtual Environment For Python Development

This article covers how we can use virtual environments to work with different versions of Python for different projects.

Introduction 

In this article, we will start off by installing Python 3.6 and later, we will set up various virtual environments to work on different projects that require different versions of Python (say 2.7 and 3.6). Just for a change (honestly, I'm bored working on Windows), I will be doing everything on a system running Ubuntu 16.04.

If you are interested to install Ubuntu alongside your Windows system, here is an article that shares clean and detailed steps. In case it doesn't work for you, just ask Google and you will have plenty of articles to help you out. Now, let's get started.

 
Installing Python and pip

Ubuntu 16.04 comes with Python 2.7 pre-installed, and you can check the Python version using either of the following commands over a terminal.
  1. python --version  
  2. python -V 
In order to install Python 3.6, we are required to run the following commands and when asked for, input the sudo password.
  1. sudo add-apt-repository ppa:jonathonf/python-3.6  
  2. sudo apt-get update  
  3. sudo apt-get install python3.6  
If you don't get any errors, you can now run the following commands to ensure that both the versions of Python are now available.
  1. python --version  
  2. python3 --version  
Now that we have multiple versions of Python available, it's time to install pip, the package management system used to install and manage software packages written in Python. In order to install pip, run the following commands. The first command installs pip, and the later one upgrades pip to its latest version.
  1. sudo apt install python-pip
  2. sudo pip install --upgrade pip
The Virtual Environment

Virtual environments are very useful because they prevent package clutter and version conflicts in the system’s Python interpreter. Creating a virtual environment for each application ensures that applications have access to only the packages that they use, while the global interpreter remains neat and clean and serves only as a source from which more virtual environments can be created.

To set up the Python virtual environment, we are required to install a package called virtualenv. Let's do that now.
  1. pip install virtualenv --user
Alright! Now, we have all the tools to create our virtual environments for different projects as needed. The following commands let us create our first virtual environment. I have put comments in front of each command to explain what it does.
  1. // brings you to your desktop, no matter where you currently are  
  2. cd ~/Desktop

  3. // creates a directory 'project-one' on your desktop  
  4. mkdir project-one

  5. // change current directory from ~/Desktop to ~/Desktop/project-one  
  6. cd project-one

  7. // creates a virtual environment named 'venv', you can name it whatever you want  
  8. virtualenv venv
After you run the last command, you should see something similar to the below screenshot. The command creates a directory as venv inside project-one, and installs the packages pip, setuptools, and wheel.

 

Now that we have our virtual environment created, it's time to activate it and see what we have got here. In order to activate a virtual environment, we use the following command.
  1. source venv/bin/activate   
 
 
The venv command shows that our virtual environment is now active. If you gave your environment a different name than mine, then you should see your environment there as (<your_environment_name>) in your console, right before the hostname. We can then run the pip list command to see packages installed along with their current version.

Python Version 

If we now run the python --version in the terminal, we see that our environment uses the system Python version, which should be Python 2.7 (if you have not updated it). However, we have installed another version of Python in our previous steps. So, the question is how do we make our virtual environment use the latest version.
 
We can do so by specifying the Python that we want our environment to use it at the time of its creation using the following commands. 
  1. // brings you to your desktop, no matter where you currently are
  2. cd ~/Desktop/

  3. // creates a directory 'project-two' on your desktop
  4. mkdir project-two

  5. // change current directory from ~/Desktop to ~/Desktop/project-two
  6. cd project-two

  7. // creates a virtual environment named 'venv', with Python 3.6
  8. virtualenv venv -p /usr/bin/python3.6

  9. // activates the virtual environment  
  10. source venv/bin/activate

  11. // verify the Python version
  12. python --version 
 
 
If nothing goes wrong, you too should see a similar output as in the above image. 
 
Summary 
 
In this article, we saw how we can work with different versions of Python for different projects using virtual environments. But I must tell you that we have barely scratched the surface with pip and virtual environments because there is much more to it. I will soon write another article that will cover pip in more details.