How to Create a Virtual Environment in Python

an environment with a particular python version and some additional packages

Amit Chauhan
The Pythoneers

--

Photo by Markus Spiske on Unsplash

What is a virtual environment?

A virtual environment is a self-contained directory tree that contains a particular Python version and some additional packages.

In other words, a virtual environment is a tool that helps us to create different python environments for various python projects to keep their dependencies separated. This is one of the most important tools preferred by Python programmers.

Why do we need a virtual environment?

Say, for example, we have multiple projects that depend on a single package like Django or Flask. Each of these projects might be using a different version of Django or flask.

Now, if you upgrade any of these packages and the global site packages, it would break a few of your websites.

If each of those projects had an isolated environment; where they only have specific versions, packages, and dependencies, it would be much more efficient. This is how we know a virtual environment is necessary if, at a time, you are working on multiple projects, as it allows us to create different python environments.

--

--