DEV Community

Cover image for How to create a Python virtual environment using virtualenv
Adnane Kouna
Adnane Kouna

Posted on • Originally published at techdemon.hashnode.dev

How to create a Python virtual environment using virtualenv

Introduction

What is a Python virtual environment?

A Python virtual environment 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 for months, and you now want to start another project but everybody is talking about how cool Django 4 is. So what do you choose? Would you keep Django 3 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 has been the default module allowing developers to create and manage virtual environments. However, many Pythonistas still prefer using virtualenv 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 :

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Note :

  • Use your own project's directory instead of '/home/.../CoolProject1'.

Next, we execute the command below :

virtualenv project-virtualenv
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • For Windows:
project-virtualenv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 library.

If you liked this article, don't forget to subscribe to my blog and/or leave a comment. It would be much appreciated!

You might also enjoy

Sources & References

Top comments (0)