Indepth on Virtual Env. - venv

A deep dive into Python virtual environments, using pip for web applications and conda for machine learning and data science workflows.

Aug 16, 2025

Understanding Python Virtual Environments

A Virtual Environment (venv) is an isolated Python workspace that allows developers to install and manage dependencies for each project independently.

It prevents conflicts between global and project-specific packages, ensuring your web, data science, or automation projects remain stable and reproducible.


Why Use a Virtual Environment?


Dependency Isolation: Avoid version clashes between different projects.

Cleaner System: Keeps global Python environment untouched.

Reproducibility: Share requirements.txt to recreate identical setups elsewhere.

Version Control: Use different Python or library versions per project.

Deployment Ready: Ensures consistent environment for CI/CD and servers.

Two major python's package, dependencies & environment mangaer


pip

conda


pip (Pip Installs Packages)

  • pip stands for 'Pip Installs Packages'. It is the default package manager for Python, officially maintained by the Python Software Foundation.

  • It installs and manages Python packages from PyPI (Python Package Index), the global repository of Python libraries.

  • It is considered best for web development, script automation & for general python tasks.

  • Link of PyPI packages: https://pypi.org/


Conda

  • conda is a cross-language package, dependency, and environment manager developed by Anaconda, incorporation.

  • It’s not just for Python β€” it can manage C, C++, R, Java, etc. dependencies too.

  • Unlike pip, conda can even install Python itself, meaning you can setup/manage your desire isolated versions of Python.

  • Conda installs packages from the Anaconda Repository (or Conda-Forge).

  • It is considered best for Data science (DS), Machine Learning (ML) & for cross-language environments.

  • Link of conda packages: https://anaconda.org/ & https://conda.anaconda.org/conda-forge/



Setting up via, pip (Pip Installs Packages)


πŸš€ Setting Up Virtual Environment in Windows OS (Using virtualenv)


Step 1(Only one time): Open Windows PowerShell as Administrator

One time Command - Only required once if virtualenv not installed previously

To check if virtualenv is already intalled/setup on your windows OS or not.

virtualenv --version


If only not have already installed, then you can run the following command to install it.
No need if already installed.

pip install virtualenv


Step 2: Create a Virtual Environment

At first, navigate (go to) to your project directory

cd path_to_your_project

Now, You create python virtual environment

Syntax:

virtualenv virtual_env_folder

For Example:

virtualenv python_env


Step 3: Activate the Virtual Environment

Syntax:

virtual_env_folder\Scripts\activate

For Example:

python_env\Scripts\activate

Once activated, you’ll notice (python_env) appears before your command line β€” indicating your environment is active.

πŸ’¬ To deactivate: deactivate



🐧 Set Up Virtual Environment in Linux OS (using python-venv)

(Linux Destros like Ubuntu, Arch Linux, Fedora, Pop!_OS)



Step 1(Optional): Install venv module only if not already setup on your system

(One-time Setup)

First, check if python-venv is already intalled/setup on linxu os or not.

(If already installed, no need to install again.)


apt list --installed | grep python3.13-venv

OR

dpkg -l | grep python3.13-venv


Now, check which python version is in your linux operating system.

python3 --version OR python --version

python3 --v OR python --v

which python3 OR which python

The venv module may not come preinstalled with Python, so install it first.


Syntax:

sudo apt install python-venv

For Example:

sudo apt install python3.13-venv


Step 2: Create a Virtual Environment Folder

Syntax:

python3 -m venv virtual_env_folder

For Example:

python3 -m venv python_env


Step 3: Activate the Virtual Environment

Syntax:

source virtual_env_folder/bin/activate

For Example:

source python_env/bin/activate

Once activated, the environment name appears at the beginning of your terminal prompt.

To deactivate: deactivate



🧱 Create Python virtual env. offline using .whl(Wheel files)


Step 0.1: Should have prior(already) downloaded all required Packages and Dependencies

(In another computer where internet is available)

Syntax:

pip download package_name --dest /path/to/folder

For Examples:

pip download numpy --dest ./packages

pip download pandas --dest ./packages

pip download matplotlib --dest ./packages

pip download seaborn --dest ./packages

pip download scikit-learn --dest ./packages

pip download jupyter --dest ./packages

This saves all required wheel files (.whl) and their dependencies to your specified folder(eg: packages).


Step 0.2 (Optional): Install venv Module only if not in your OS (One-time Setup)

To check current python version in your OS

python3 --version OR python3 --v

Syntax:

sudo apt install python-venv

For Example:

sudo apt install python3.13-venv


Step 1: Create a Virtual Environment Folder

Syntax:

python3 -m venv virtual_env_folder

For Example

python3 -m venv python_env


Step 2: Install Packages Offline

Syntax:

pip install --no-index --find-links=./packages package_name

For Example:

pip install --no-index --find-links=./packages numpy

pip install --no-index --find-links=./packages pandas

pip install --no-index --find-links=./packages matplotlib

pip install --no-index --find-links=./packages seaborn

pip install --no-index --find-links=./packages scikit-learn

pip install --no-index --find-links=./packages jupyter

Here --no-index prevents pip from connecting to PyPI, and --find-links specifies where your .whl files are located.

πŸ“„ Alternative way Installation using requirements.txt

If you have a list of major packages in requirements.txt, the u can use:

pip install --no-index --find-links=./packages -r requirements.txt



PIP Bonus Commands :

πŸ“„ To include all packages dependencies in requirements.txt file in your project

pip freeze > requirements.txt

πŸ“„ To Recreate the same environment on another system (another computer):

pip install -r requirements.txt

πŸ” To check Dependencies of Any Package

pip show package_name | grep Requires



Setting up via, conda

To use "conda" package manager:

At first, u either have to install "Miniconda" OR "Anaconda"



Installing Miniconda on Linux OS (Ubuntu):


1. For viewing the Miniconda installer:

Official site link to download miniconda installer:

https://www.anaconda.com/docs/getting-started/miniconda/install
https://repo.anaconda.com/miniconda/

Select the latest version miniconda for your OS, and right click on that link & copy the link address


2. To download the installer:


Open your terminal & run the following commands:

Syntax: wget copied_link_address

Example: wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

Press "Enter"

NOTE: Miniconda installer file is downloaded in Home directory


3. To install the downloaded installer, run the following command:

Syntax: sh Miniconda-latest-Linux-version.sh

Example: sh Miniconda3-latest-Linux-x86_64.sh

Press "Enter"


4. Follow the prompts on the installer screens:


If you are unsure about any setting, accept the defaults. You can change them later.

Steps:

  1. press "Enter" to read license aggreement

  2. Then, press 'q' if u don't want to read all of that

  3. Then, type "yes" and press "Enter", to accept the license term


Now, a message will be prompted on screen saying:

"Miniconda3 will now be installed into this location:

/home/username/miniconda3"

press "Enter"


Now, a message will be prompted on screen saying:

"Do you wish the installer to initialize Miniconda3 by running conda init? [yes|no]"

type "yes" and press "Enter"


5. To verify conda installation:

SYNTAX: source /home/username/.bashrc


Example: source /home/dhiraj/.bashrc

Press "Enter"

Hopefully, if everything has gone well, then u will be on conda (base) environment on terminal

NOTE: You can deactivate conda (base) environment using command:

conda deactivate


NOTE: You can activate conda(base) environment using command:

conda activate base



To setup conda virtual environment in Linux(Ubuntu):


1. At first u should have already setup miniconda/anaconda in you pc.

(conda package manager should already be setup in your laptop)


2. Open a terminal and create a new conda virtual environment.

SYNTAX: conda create -n venv python=version

Example: conda create -n python_env python=3.10


3. Activate the above created conda virtual environment.

Example: conda activate python_env


4. Install any conda packages u want

Syntax: conda install package_name

OR

Example: conda install pandas


5. Deactive conda environment

conda deactivate



Conda Bonus Commands


  1. Command to create conda virtual environment

Syntax: conda create --n venv

Example: conda create --n python_env


  1. Command to create conda virtual environment with specific "python version"

Syntax: conda create --n venv python=version

Example: conda create --n python_env python=3.13.3


  1. Comamnd to create virtual env & install all dependencies/packages/requirements from a .txt/.yml file

Syntax: conda env create -n venv --file environment.txt

Example: conda env create -n python_env --file environment.txt

OR

Syntax: conda env create -n venv --file environment.yml

Example: conda env create -n python_env --file environment.yml


  1. Activate conda virtual environment

Syntax: conda activate venv

Example: coda activate python_env


  1. Deactivate conda virtual environment

conda deactivate


  1. To Install a specific package

To install a specific package

Syntax: conda install package_name

Example: conda install pandas


To install a package with a specific version

Syntax: conda install package_name=package_version

Example: conda install jupyterlab=4.0.3


  1. List out all the packages installed on any virtual environment

conda list


  1. List out all virtual environments currently managed by conda

conda env list



🏁 Conclusion


  • Virtual environments are non-negotiable tools for Python developers.

  • Whether you’re building web apps with Django, data pipelines with Pandas, or ML models using TensorFlow, mastering venv ensures your workflow remains clean, portable, and reliable.

  • Code runs smoothly when environments are isolated.

  • The cleaner your virtual environment, the more predictable your code behavior.



WALLAAHH !!