Artificial Intelligence

Practical Guide for Virtual Environments in Python

Using virtualenv and pipenv tools

Soner Yıldırım
January 3, 20214 min read
Photo by Markus Spiske on Unsplash [2].
Photo by Markus Spiske on Unsplash

The projects we work on are very much likely to have many dependencies that need to be installed. These dependencies facilitate many tasks in projects. However, we need to be careful about them especially when working on multiple projects.

Just like any other technology, the software packages or programming languages are constantly being improved. Thus, new versions are being introduced.

Different projects might require different versions of a package or software. For instance, we might have one project that requires Python 2.7 and another one with Python 3.6. As the number of projects and dependencies increase, it becomes hard to follow up and handle such differences.

One way to overcome this issue is to use virtual environments. They can be considered as bounding boxes for software packages. We can develop a project in a virtual environment and install all the dependencies specific to that project. What we have in the virtual environment is not affected by the changes in the global scope of our machine.

There are many virtual environment tools for Python such as pipenv, virtualenv, venv, and so on. In this article, we will go over some examples using virtualenv and pipenv to get familiar with the idea of virtual environments and how they work.

Let's start with the virtualenv. We first install it from the terminal using python package installer (pip).

text
$ pip install virtualenv

We create a sample project file as our working directory.

text
$ mkdir demoproject$ cd demoproject

We are now inside the demoproject directory. We will create a virtual environment using the following command.

text
$ virtualenv venv_demo

It's been created. We can run the ls command to see the files in the current working directory.

text
$ lsvenv_demo

The next step is to activate the virtual environment.

text
$ source venv_demo/bin/activate

Once the virtual environment is activated, its name is displayed in the terminal as below:

(image by author)
(image by author)

We can now install packages.

text
$ python -m pip install pandas

We now have pandas installed in our virtual environment. The freeze command shows the list of installed packages.

text
$ python -m pip freezenumpy==1.19.4pandas==1.1.5python-dateutil==2.8.1pytz==2020.5six==1.15.0

NumPy has also been installed because it is a dependency for Pandas. The installed version of Pandas is 1.1.5. We can specify the version we need while installing a package.

text
$ python -m pip install pandas==1.0.5

If you just want to check the installed version of a particular package, the freeze command is used with grep:

text
$ pip freeze | grep pandaspandas==1.0.5

We can also install several packages saved in a text file. It is better than installing dependencies one-by-one especially when there are several of them.

text
$ python -m pip install -r requirements.txt

In order to exit the virtual environment, we use the deactivate command.

text
$ deactivate

The next tool we will discover is pipenv which can be installed using the pip:

text
$ pip install pipenv

Let's create a new virtual environment using pipenv.

text
$ pipenv install --python=/usr/bin/python3.6

Pipenv allows for installing a dependency while creating the virtual environment. For instance, I could have added pandas at the end of the command above so that the virtual environment is created with pandas installed.

We run the shell command to activate the virtual environment.

text
$ pipenv shell
(image by author)
(image by author)

We are now in the virtual environment. Let's also install pandas to this one.

text
$ pipenv install pandas

The graph command shows a detailed overview of the installed packages.

text
$ pipenv graph
text
pandas==1.1.5  - numpy [required: >=1.15.4, installed: 1.19.4]  - python-dateutil [required: >=2.7.3, installed: 2.8.1]    - six [required: >=1.5, installed: 1.15.0]  - pytz [required: >=2017.2, installed: 2020.5]

We can uninstall a specific package or all the packages in the virtual environment using the uninstall command.

text
$ pipenv uninstall pandas 

The following command uninstalls all the packages.

text
$ pipenv uninstall -all

We type the "exit" command to exit the virtual environment.


Conclusion

Virtual environments are great tools to manage multiple projects at the same time. There are numerous packages and libraries which are updated in no time. Thus, it is exhaustive and not efficient to try to manually follow up.

What we have covered in this article can be considered as a practical introduction to Python virtual environments. There is, of course, much more to learn both theoretically and practically.

The official documentation of virtualenv and pipenv provide more detailed overview about these tools.

Thank you for reading. Please let me know if you have any feedback.

Related Articles