How to create a virtual environment in Python?

In this article, we will see how to create a virtual environment in python using python commands. Packages and extra modules that aren’t included in the standard Python library are frequently used in Python applications. Applications may occasionally require a specific version of the library to complete a job or resolve a bug.

The importance of create a virtual environment in Python

Assume we’re working on two apps that require an older version of the library, but other applications in the same Python installation require a newer version of the library. It can sometimes result in a problem.

To put it another way, a single Python installation might not be able to meet the needs of every application. The various apps’ requirements may clash with one another. For example, if one program requires version 1.0 and another requires version 2.0, the two will clash, resulting in one application being unable to execute.

Python provides to establish a virtual environment to satisfy this conflicting requirement. Version 1.0 is the environment for Application A. Because application B’s environment is 2.0, upgrading it to version 3.0 will have no effect on application A’s environment.

Let’s Create a Virtual Environment

A virtual environment is created and managed using the venv module. It’s compatible with the most recent Python version. We may choose where to establish a virtual environment and use the directory path to run the venv module as a script. In your command prompt, type the following command and press enter.

python -m venv new-env  

After entering this command you will see a pop-up just click yes to create a virtual environment

one

The command above generates the new-env directory, as well as a directory within the newly constructed virtual environment new-env, which contains a new copy of the Python interpreter.

two

All virtual environments are saved in the venv folder, which is a shared directory. After we’ve created the virtual environment, we’ll need to activate it with the command below.

On Window, Press enter after typing:

new-env\Scripts\activate.bat 
three

On UNIX or MacOs, run:

source new-env/bin/activate  

After activating the virtual environment, the shell prompts will change to indicate the virtual environment we are using.

Managing Packages Using pip

Using the Python package manager pip, we may install, update, and uninstall libraries in an enabled virtual environment. For example:

(new-env) E:\wheather app> pip install requests==2.6.0  
Collecting requests==2.6.0  
Using cached requests-2.6.0-py2.py3-none-any.whl  
Installing collected packages: requests  
Successfully installed requests-2.6.0  

If you want more details of PIP then read our last article on PIP.

That’s all for this article if you have any confusion contact us through our website or email us at [email protected]com or by using LinkedIn

If you want to learn more about the python programming language then click the link to see our last article How To Install PIP to Manage Python Packages On Windows

Leave a Comment