The first python project I completed was not a great one but it did what it was supposed to. A few days ago, I needed to use the project. And when I tried to run it, there were a lot of bugs now. A few years ago, it ran perfectly. It took me a few hours to get it working again.

The reason this happened is because I had not used a virtual environment when I completed it.

What is Virtual Environment?

When python and its tools (including libraries) are installed, they are installed in a standard location (for eg. /usr/bin) by the system (might be an OS) and this is where the python interpreter and libraries are searched for when you run python scripts. Virtual environment is an isolated setup of python executable and libraries, isolated as in separated from the standard locations mentioned above. When you run a python script inside virtual environment, python interpreter and libraries are searched in the virtual environment.

Why use Virtual Environment?

Lets look at the advantages of using virtual environments.

  1. When the system is updated, python and its libraries in the standard locations are also updated. Projects which use older versions of Python or its libraries may behave unexpected. This is what caused my problems.
  2. The versions of Python and its libraries can be chosen inside the virtual environment and these do not get updated when the system is updated. So, even the older versions can be used as isolated run-time environments.
  3. You can use multiple versions of Python for multiple projects using separate virtual environments. Using standard system version can only allow use of one Python version.
  4. The requirements of the projects with their individual versions can be easily saved in a file from a virtual environment which helps to recreate the same run-time environment on other systems too.

How to start Virtual Environment?

First you need to have Python installed on your system. If you need help on this, take a look here.
Virtual Environment can be started with Pipenv or Virtualenv.

Using virtualenv

First install virtualenv if not installed:

Then start a virtual environment with this command:

You can specify which python version to use like this:

This will install the specified python interpreter and standard libraries for this python version in the folder my_venv as follows:

Now, you have to activate this virtual environment to instruct the system to use the interpreter from this environment instead of standard one. This can be done like this:

If you are on Windows, the activation script should be on the Scripts folder instead of bin

A clear indication of whether the virtual environment has been activated or not will appear in the command line. On some system it may appear as:

Now pip can be used to install or uninstall the required dependencies in the virtual environment.

To see, what dependencies are install in the virtual environment, following can be used:

Execute this command to save the dependencies status of virtual environment to a file.

This will create a file requirements.txt with dependencies and their versions. This file can be used to recreate virtual environment elsewhere too. This can be done by:

When you’re done working in the virtual environment and want to use the system’s default python, you need to deactivate the virtual environment.

This can be verified as the virtual environment indicator will dissappear now.

Using pipenv:

pipenv is higher level abstraction and uses virtualenv in the background.

First install pipenv with:

This installs pipenv on the system. To install packages to the project, change directory to the project and start pipenv shell.

To uninstall a package:

When pipenv install command is used for the first time, two new files will be created: Pipfile and Pipfile.lock file. The dependencies you install in the virtual environment and their versions are stored in these files which can be used to recreate this environment later using this command:

pipenv has a lot more functionalities which lie outside the scope of this article.

So, don’t make the mistakes that I made, always use virtual environments.