Open In App

Python Virtual Environment | Introduction

Last Updated : 20 Dec, 2023
Like Article

A Python Virtual Environment is an isolated space where you can work on your Python projects, separately from your system-installed Python.

You can set up your own libraries and dependencies without affecting the system Python.

We will use virtualenv to create a virtual environment in Python.

What is a Virtual Environment?

A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated Python virtual environments for them. This is one of the most important tools that most Python developers use.

Why do we need a virtual environment?

Imagine a scenario where you are working on two web-based Python projects one of them uses Django 4.0 and the other uses Django 4.1 (check for the latest Django versions and so on). In such situations, we need to create a virtual environment in Python that can be really useful to maintain the dependencies of both projects.

When and where to use a virtual environment?

By default, every project on your system will use these same directories to store and retrieve site-packages (third-party libraries).

How does this matter? Now, in the above example of two projects, you have two versions of Django. This is a real problem for Python since it can’t differentiate between versions in the “site-packages” directory. So both v1.9 and v1.10 would reside in the same directory with the same name.

This is where virtual environments come into play. To solve this problem, we just need to create two separate virtual environments for both projects.

The great thing about this is that there are no limits to the number of environments you can have since they’re just directories containing a few scripts.

A virtual Environment should be used whenever you work on any Python-based project. It is generally good to have one new virtual environment for every Python-based project you work on. So the dependencies of every project are isolated from the system and each other.

Create Virtual Environment in Python

We use a module named virtualenv which is a tool to create virtual environments in Python, isolated from system environment Python.

virtualenv creates a folder that contains all the necessary executables to use the packages that a Python project would need.

Installing virtualenv

$ pip install virtualenv

Test your installation:

$ virtualenv --version

Create a new Virtual Environment

You can create a virtualenv using the following command:

$ virtualenv my_env

After running this command, a directory named my_env will be created. This is the directory that contains all the necessary executables to use the packages that a Python project would need.

This is where Python packages will be installed. If you want to specify the Python interpreter of your choice, for example, Python 3, it can be done using the following command:

$ virtualenv -p /usr/bin/python3 virtualenv_name

Activating a Virtual Environment in Python

Now after creating a virtual environment, you need to activate it. Remember to activate the relevant virtual environment every time you work on the project. This can be done using the following command:

Activate a Virtual Environment on Windows

To activate virtual environment using windows command prompt change directory to your virtual env, Then use the below command

$ cd <envname>
$ Scripts\activate

Note: source is a shell command designed for users running on Linux (or any Posix, but whatever, not Windows).

Activate a virtual environment on Linux

$ source virtualenv_name/bin/activate

Once the virtual environment is activated, the name of your virtual environment will appear on the left side of the terminal.

activate virtual environment in Python

This will let you know that the virtual environment is currently active.

Installing Dependencies in Virtual Environment Python

In the image below, venv named virtual environment is active. Now you can install dependencies related to the project in this virtual environment.

For example, if you are using Django 1.9 for a project, you can install it like you install other packages.

(virtualenv_name)$ pip install Django==1.9

The Django 1.9 package will be placed in virtualenv_name folder and will be isolated from the complete system.

Deactivate Python Virtual Environment

Once you are done with the work, you can deactivate the virtual environment by the following command:

(virtualenv_name)$ deactivate

deactivate virtual environment in Python

Now you will be back to the system’s default Python installation.

We have covered virtual environment in Python, How to create virtual environment in Python?, how to activate and deactivate virtual environment? and now to install dependencies.

This covers all the basic concepts of Python virtual environment and you can use it on your personal PC.

Also Read:

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads