Virtual Environment in Python

A Virtual Environment is a functioning clone of the Python environment that has been isolated, allowing you to work on a single project without being concerned about any dependencies with other projects.

Why is a virtual environment necessary?

As an illustration, project A that you are working on needs Python library A (site package) version 1.0, whereas project B needs the same library A but the more recent version 1.3. A virtual environment can be conducive in such circumstances to manage the dependencies of both projects.

Let's use the identical illustration once more to comprehend why. then how do we use this?

As an illustration, you might be working on project A, which needs Python's library A (site package), version 1.0, whereas project B, which requires the same library A, but version 1.3, is more recent.

Given that Python cannot distinguish between different versions in the site-packages directory, this is a severe issue. Therefore, the same directory with the same name would house both v1.0 and v1.3.

Versions are not distinguished because projects are only stored based on their names. As a result, it would be necessary for Project A and Project B to use the same version, which is frequently unacceptable.

The use of virtual environments and the virtualenv/venv tools are necessary for this situation.

Creating a Virtual Environment: Steps

Step 1: Construct

Choose a directory to put your virtual environment in, then run the venv module as a script using the directory path:

Note: The venv module from the standard library should already be installed if you are using Python 3.

Since there aren't many differences between the newer venv tool and virtualenv in terms of the actual commands, we'll assume you're using it moving forward. They are actually very different tools, though.

> python3 -m venv [Virtual Environment Name]Example,
> python3 -m venv sample_venv

Note: By default, none of your current site packages will be included in this.

Here, the Python -m flag denotes the module, i.e., some modules have main entry points that must be used in order for them to function.

If the sample venv directory doesn't already exist, it will be created along with subdirectories that house copies of the Python interpreter, the standard library, and various supporting files.

Step 2: Turn on

We must activate the virtual environment before we can begin installing our packages there. Every time you work on the project, don't forget to activate the pertinent virtual environment. The command listed below can be used to accomplish this:

On Windows :

> .\[Virtual Environment Folder Name]\Scripts\activateExample,
> .\sample_venv\Scripts\activate

On Unix or Mac :

> source [Virtual Environment Name]/bin/activateExample,
> source sample_venv/bin/activate

The name of your virtual environment will appear on the left side of the terminal as soon as it is activated. You will be informed that the virtual environment is active by this.

Deactivating the virtual environment

So in this way you can create, activate and deactivate your virtual environment.

I hope you like this


119 views0 comments

Recent Posts

See All