Python Virtual Environment : Secrets Revealed
What is Python Virtual Environment
A Python virtual environment is a self-contained directory that isolates Python installations and dependencies for different projects. which can also be said as directory which also has its own python interpreter and all the required packages.
Benefits of Python Virtual Environment
Isolation: Creates a separate Python environment for each project, preventing conflicts between different versions of packages.
Dependency Management: Allows you to install and manage project-specific dependencies without affecting your system-wide Python installation.
Experimentation: Enables you to try out different versions of packages or test experimental code without affecting your system-wide Python installation.
Create Python Virtual Environment
Now we come to our main Agenda, above you can find anywhere on the internet.
let's hop into the process of creating python virtual environment on your machine.
The Command
python -m venv examplenv
This command consist of different parts. let's see breakdown of command.
The first argument i.e.
python
calls out the python interpreter installed in your system. for some users this might bepython3
in my case it is python.The
-m
flag is called the module flag. It tells Python to treat the following argument, in this case,venv
as a module or package name.The venv is the name of package to be called.
examplenv is the name of virutal environment and it can be anything. most of the people use venv as their virtual environment name.
When we run this command in the terminal the venv package gets called from python interpreter with argument examplenv.
Configurations and directory informations
Directory Creation: You will see the directory named examplenv got created in your current folder and it contains different directories and configurations and structure of the directory is as show below
examplenv/
├── Scripts/
│ ├── activate.bat
│ ├── deactivate.bat
│ ├── pip.exe
│ ├── python.exe
└── Lib/
│ ├── site-packages/
│ ├── pip/
│ ├── setuptools/
│ ├── pkg_source/
└── pyvenv.cfg
Let's discuss each directory listed above.
Scripts or bin directory contains the newly installed python interpreter and pip (package manager) for examplenv it contains all executables and scripts.
a] activate.bat has script for activating the virtual environment in this
we have script which sets the new python path has been set to environment variables.
b] deactivate.bat contains script for deactivating the active virtual environment.
c] python.exe personal python interpreter for examplenv.
d] pip.exe is personal package manager for managing the dependencies at examplenv.
Lib directory contains all the installed packages with the help of new pip package manger and it will be only used and accessed within this examplenv.
pyvenv.cfg this configuration file contains the path and details of the parent interpreter i.e. from which this new virtual environment got created.
Activate virtual environment
for windows
examplenv\Scripts\activate.bat
for linux or mac
source examplenv\bin\activate
We are accessing the Scripts or bin directory and we are running our script written inside activate file, this will configure new python path in your environment variable.
Python Interpreter
python --version
After activating the virtual environment if you check the version of python it will call this local python interpreter residing inside examplenv and gives the version of that python interpreter.
Package installation
pip install package_name
write your required package name at package_name
. Now, when we install using this pip (package manager of examplenv) all the packages installed inside examplenv/lib/site_packages. this dependency will not get installed in parent python interpreter.
Deactivate Virtual Environment
deactivate
we need to just type deactivate in our terminal and it will deactivate the virtual environment. as we can see for activating we use examplenv\Scripts\activate
but for deactivating we have only used deactivate
the reason is while activating the virtual environment we were outside of the virtual environment directory. now as we're already inside the virtual environment directory we just need to use deactivate
command which runs deactivate.bat file.
Thanks for reading folks!
Subscribe to my newsletter
Read articles from Ashraf Malik directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by