How to create a virtual environment for Python

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 need to 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.

In this blog will learn about how to create Virtual Environment for your project.

How to create Virtual Environment?

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

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 venv

For a Python 3 virtual environment type -

virtualenv venv -p python3

By default, In python venv comes, so you can also use like:

python3 -m venv venv
In your machine, If only python3 are installed then, we can also use like:
virtualenv venv

This will also work

python -m venv venv

This will create a directory venv 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 venv>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:

venv\Scripts\activate.bat

On Unix or MacOS, run:

source venv/bin/activate

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

(venv)Environments $ 

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

deactivate

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

Explore More Python Posts

Using Twitter API with Python: Getting Tweets & Insights

Learn how to use the Twitter API with Python to get tweet information and insights. Extract valuable data for businesses and researchers with ease.

Read More
Accessing Facebook Data with Python: Examples for Post Likes, Views, and Profile Details

Learn how to access Facebook data using Python and the Facebook API. Get post likes, views, and comments, and retrieve profile details.

Read More
Python Design Patterns: Examples and Best Practices

Learn about Python design patterns with examples and discover best practices for writing maintainable and scalable code.

Read More
How to Use the YouTube API with Python: A Step-by-Step Guide

Learn how to access and retrieve information from YouTube using Python and the YouTube API. Get code examples and step-by-step instructions for impor…

Read More
Top 3 Python Frameworks for Web Development

Discover the most popular and efficient Python frameworks for building dynamic web applications. Get started today!

Read More
Debugging Made Easy with IPDB - The Python Debugger

Revolutionize the way you debug your Python code with IPdb, the advanced interactive debugger that streamlines your debugging process. Say goodbye to…

Read More