Python | Create virtual environment using venv

In this article, we will be learning how to create a virtual environment using venv in Python. First, let us see on few important topics before creating one virtual environment.

Introduction:- A virtual environment is used to isolate the projects which have different dependencies requirements. Few projects may require a specific version, different packages, or specific modules. Therefore, installing and importing everything in one place for various Python projects may lead to errors and dependency problems.

Process of virtual Environment:- Virtual environment create a separate folder apart from the parent Python folder along with the site-packages and other required dependencies for the project. This folder consists of Python with its modules and required dependencies of specific version particular to the project. This creates a separate environment for the project along with other various virtual environments without clashing with other projects.

Moreover, multiple virtual environments can exist in the same Python folder without affecting other projects. To create a virtual environment using venv follow the steps below.

Create a virtual environment using venv in Python

Virtualenv is a tool that creates a virtual environment for older versions of Python. In Python 3, replace virtualenv with venv.

First, we will update pip for the latest modules and upgradations in the dependencies:-

Windows-

py -m pip install --upgrade pip

MAC OS / LINUX-

python3 -m pip install --user --upgrade pip

After this, the latest version of pip will be installed.

 

Secondly, to install virtualenv, execute the below command:-

Windows-

py -m pip install --user virtualenv

MAC OS / LINUX-

python3 -m pip install --user virtualenv

Therefore, the venv is successfully installed.

 

Moving further, let us create a virtual environment:-

Windows-

py -m venv env_name

MAC OS / LINUX-

python3 -m venv env_name

This creates a virtual environment with the name env_name.

 

Consequently, the virtual environment is created. Now set the environment as active to start working on your new environment:-

Windows-

.\env\Scripts\activate

MAC OS / LINUX-

source env/bin/activate

As a result, the following output is obtained:-

C:\Users\KIRA>.\env_name\Scripts\activate
(env_name) C:\Users\KIRA>

Subsequently, your new virtual environment is now active. You can confirm it by seeing the name of your environment beside the terminal prompt.

 

However, If you want to know your current location, execute the following commands:-

Windows-

where python
.../env/bin/python.exe

MAC OS / LINUX-

which python
.../env/bin/python

The location:-

(env_name) C:\Users\KIRA>where python
C:\Users\KIRA\env_name\Scripts\python.exe
C:\Users\KIRA\AppData\Local\Programs\Python\Python37\python.exe
C:\Python27\python.exe

 

However, if you wish to leave the current virtual environment and want your system Python interpreter, deactivate the environment by executing the following command:-

deactivate

As a result, this will take us back to the system Python environment.:-

(env_name) C:\Users\KIRA>deactivate
C:\Users\KIRA>

 

In conclusion, I hope you liked the article. Feel free to comment below for any queries.

Leave a Reply

Your email address will not be published. Required fields are marked *