Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

How To A Create Virtual Environment for Python

4 min read

A virtual environment is a self-contained directory tree that contains dependencies required by different projects isolated to existing packages.

By using virtual Python environments, applications can run in their own ‘sandbox’ in isolation of other Python applications

This lets you have an isolated space on your computer for different Python projects, ensuring that each of your projects can have its own set of dependencies and modules that won’t disrupt any of your other projects. This is among the most essential tool in the bucket of any Python developer and you will use it quite often.

Why use Virtual Environments?

Let's take an example of our favorite web framework Django. Before the release of Django 2.0, all Django projects were made on the top of Python 2 which is the legacy version of Python after the Django 2.0 update Django only works with the latest Python 3 which is addressed as the future of Python.

Both the versions of the framework depends on different dependencies in such scenarios it is advisable to work with the virtual environment to set up an independent environment for projects on different versions of Django.

The virtual environment creates a directory that contains dependencies required by different projects along with some scripts. There are no limitations on the numbers of environments you can create.

Now let's take another real-world example, Imagine you have two projects A and B which depend on module C. The problem appears when we need different versions of module C, let's say module A demands v 1.0 and project B demands V 3.0 for some compatibility reason.

But python won't be able to differentiate between the different versions of a module because both the version will be stored in site package directory with the same name.

This is where you should use the virtual environment to create sperate virtual environments for both the projects, different environments can have different versions of modules.

It is a good practice to have a new virtual environment for every Python project.

This tutorial will guide you through how to create Virtual Environment for your project.

How to create Virtual Environment?

Step - 1

Open your terminal and create a directory to store all your virtual environments, using the command mkdir Environments which is an acronym of "make directory".

Now go inside the directory using the command CD which stands for call Directory, CD Environments

Step 2

Now we will use a module named virtualenv to create isolated virtual environments.

But first, let's install this module by the following command,

pip install virtualenv

If you get an error like pip command not found then you have to install pip package manager first, you can learn this here.

To verify a successful installation run this

virtualenv --version

Now we can proceed to create virtual environments

We will create a virtual environment named myenv.

How to create Virtual Environment if you have two different versions of Python installed in your machine?

To create a Virtual Environment for Python 2.x do the following

virtualenv myenv

For a Python 3 virtual environment type -

python3 -m venv myenv

If you only have Python 3 on your machine do the following

virtualenv myenv

This will also work,

python -m venv myenv

This will create a directory myenv along with directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.

A virtual Python environment has a similar directory structure to a global Python installation. The bin directory contains executables for the virtual environment, the include directory is linked to the global Python installation header files, the lib directory is a copy of the global Python installation libraries and where packages for the virtual environment are installed, and the shared directory is used to place shared Python packages.

The interesting thing here is that these environments  won't have any existing Python package or module of your computer, you can verify it by navigating into myenv>Lib>site-packages

You will only find the essential tools such as pip, easy install and setup tool, later this directory will hold all the packages and modules you will install in this particular environment and remain isolated from other environments.

To add modules and packages in our Environment, we need to activate it first.

On Windows, run:


On Unix or MacOS, run:

source myenv/bin/activate

Now your command prompt will be prefixed by the Environment name which is, in this case, myenv.

This indicates that our Virtual Environment has been activated.

You can install any package here with pip or easy install these packages will be completely isolated to other environments on your device.

To deactivate the environment run the following


Now there is no more a prefix in our terminal, which indicates that our environment has been deactivated successfully.


Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read