Python — Virtual Environment
Need For Virtual Environment:
Each developer will work on their development machine. Developers may associate with different projects. Each project will use a different version of the same library. If a developer installs a Python library at the system level, context switching between the projects makes it hard with a single global environment. Virtual environments isolate dependencies between projects. It allows the developer to switch between the projects without any additional steps easily.
Each project has its virtual environment, developers need to install only the project-required package. It is easier to manage the dependencies across projects.
Virtual Environment in Action:
The Module “virtualenv” is used to create the virtual environment.
Module Installation:
pip install virtualenvCreate A Virtual Environment:
Go to the root folder of your project and create a python virtual environment.
python -m venv env_nameThe above command will create a virtual environment local to your project.
Get Shankar Duraisamy’s stories in your inbox
Join Medium for free to get updates from this writer.
Activate The Virtual Environment:
env_name/scripts/activateThe above comment will activate the virtual environment. Once activated all the modules installed through the terminal will be installed and referred locally to the project. The third-party library will be installed in the below location for the project.
env_name/lib/site-packagesDe-activate The Virtual Environment:
deactivate script is used to deactivate the virtual environment.
deactivate or env_name/scripts/deactivate:How it works:
When you create a virtual environment using “venv” or “virtualenv” it sets up a directory structure that contains a copy of the Python interpreter, the pip package manager, etc. This directory structure typically looks as below:
myenv/
├── bin/ (or Scripts/ on Windows)
│ ├── activate
│ ├── python
│ └── pip
├── include/
└── lib/
└── pythonX.Y/
└── site-packages/bin/Scripts has the Executable to set the environment
lib has third-party libraries installed for your project.




