What is virtual environment and how to use it in python?
Feb. 17, 2022 by Coding India
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 is used by most developers.
Why do we need a Virtual environment?
When working with different versions of the same library or framework, we need to use a virtual environment. The virtual environment helps us to separate the different projects and their dependencies.
What do you need to do?
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.
How to Install and use Virtual Environment?
We need to install a module named — “virtualenv”. It will create a folder that contains all the necessary executables to use the packages that a Python project would need.
pip install virtualenv
Test your installation:
virtualenv — version
Creating the Virtual Environment:
virtualenv file_name
EX:- virtualenv venv
Activating the Virtual Environment:
file_name/Scripts/activate
EX:- c:\Python> venv/Scripts/activate
Now you can install any library and packages according to your project requirement and use it.
EX:- Try to install Django
pip install django
It will install Django only in your virtual environment. Deactivating the Virtual Environment for deactivating you have to just type deactivate
deactivate
Now you can start your work and enjoy it. Please visit to our Blog Page to find more interesting blogs — Coding India.