How to create a Python virtual environment using virtualenv
Tags: python, workflow, virtualenv
Published: 2022-03-15 22:44:35

## Introduction

#### What is a Python virtual environment?

A [Python virtual environment](https://docs.python.org/3/tutorial/venv.html) is an isolated development environment where the Python interpreter and all the libraries and scripts are isolated from other environments, which allows each project to have its own dependencies.

#### Why should you use a virtual environment?

If you don't find them useful yet, hear me out: Imagine you've been working on a super cool project using [Django 3](https://www.djangoproject.com/) for months, and you now want to start another project but everybody is talking about how cool [Django 4](https://www.djangoproject.com/) is. So what do you choose? Would you keep [Django 3](https://www.djangoproject.com/) and sacrifice all the new features or would you submit to your shiny object syndrome and update the library even if it means breaking your previous project(s)?

Luckily you don't have to choose between the two, thanks to virtual environments, you can install the libraries locally, hence you have the ability to install a specific version for each project.

#### Why not just use venv?
 
Ever since the release of Python 3.3.0 back in 2012, [**venv**](https://docs.python.org/3/library/venv.html#venv-def) has been the default module allowing developers to create and manage virtual environments. However, many Pythonistas still prefer using [**virtualenv**](https://virtualenv.pypa.io/en/latest/) for the reasons below:

- ***virtualenv*** is way faster than ***venv***.
- ***venv*** cannot create virtual environments for arbitrarily installed Python versions.
- You can't update ***venv*** via pip, instead, you have to wait for the next Python update.
- ***virtualenv*** is available for both Python 2 and Python 3.

#### Requirements

In this article, we're gonna show you how to install *virtualenv* in addition to how to create, activate and deactivate a virtual environment in Python. And to achieve this goal, you simply need :
- A Terminal *(for Linux or Mac OS)*.
- **Microsoft PowerShell** *(for Windows)*.
- [**Python**](https://www.python.org/downloads/).
- [**pip** package manager](https://pip.pypa.io/en/latest/installation/)

## How to install virtualenv?

To install *virtualenv*, we must use the *pip package manager*.

First, open the terminal or the PowerShell (depending on your OS) then run the following command :

```
pip install virtualenv
``` 

## How to create a Python virtual environment?

To create a virtual environment for our new Python project, we must first open the project's directory in the terminal by running the following command :

```
cd /home/tech-demon/PythonProjects/CoolProject1
```
Note : 
- Use your own project's directory instead of *'/home/.../CoolProject1'*.

Next, we execute the command below :

```
virtualenv project-virtualenv
```
**Congratulations!** You just created your first virtual environment. Notice that a new folder has appeared in the project's directory.

Note : 
- Rather than *project-virtualenv*, you can name your virtual environment whatever you want.
- If you use Git as your main version control system, then you should know that the virtual environment's directory is by default included in the .gitignore file.
- You can specify the version of Python you wanna work with using the -p flag i.e.
```
virtualenv -p pythonx.x.x project-virtualenv
```

## How to activate the Python virtual environment?

In this step, we have to run the *activator script* to activate the virtual environment.

- For Linux or Mac OS:
```
source project-virtualenv/bin/activate
```

- For Windows:
```
project-virtualenv\Scripts\activate
```

At this point, you should be able to see your virtual environment's name between parentheses on your terminal line e.g. (project-virtualenv), and from now on, all the executed Python commands and installed libraries will be inside the virtual environment until you deactivate it.

## How to deactivate the Python virtual environment?
To deactivate the virtual environment, you only need to run the following command:
```
deactivate
```

## Conclusion

To sum up this article, you've just learned about Python virtual environments, their importance to the developers' workflow, as well as how to create, activate, and deactivate one using the [*virtualenv*](https://virtualenv.pypa.io/en/latest/) library.

If you liked this article, don't forget to subscribe to my [blog](https://techdemon.hashnode.dev/) and/or leave a comment. It would be much appreciated!

#### You might also enjoy

- [10 Vim plugins I can’t live without (and how to install them?)](https://techdemon.hashnode.dev/10-vim-plugins-i-cant-live-without)

#### Sources & References

- [virtualenv's Documentation](https://virtualenv.pypa.io/en/latest/user_guide.html).
- [eResearch cookbook's Article](https://uoa-eresearch.github.io/eresearch-cookbook/recipe/2014/11/26/python-virtual-env/).
- [venv's Documentation](https://docs.python.org/3/library/venv.html).