🐍 The Essential Guide to Using Virtual Environments in Python

Bassem ShalabyBassem Shalaby
4 min read

As developers, we often navigate a world filled with packages, libraries, tools, and versions. We begin a new project with enthusiasm—perhaps a machine learning model or a web app—and soon find ourselves caught in a tangle of dependency issues.

That’s where Python’s virtual environments come to the rescue.


What Is a Virtual Environment?

A virtual environment in Python is an isolated container that allows you to manage dependencies for your project without interfering with other projects or the global Python installation.

Think of it like this: You’re building two apps. One uses Django 3.2, and another needs Django 4.0. You can’t install both globally. Virtual environments let each app have its own sandboxed space — like two apartments with different furniture and setups.


Why Should Developers Care?

Let’s look at it from our perspective — someone who codes, experiments, ships, and repeats.

Advantages

  1. Isolation of Dependencies

    • You don’t pollute your global Python with random packages.

    • Each project has its own clean environment.

  2. Version Control

    • You can use specific versions of Python or packages safely.

    • Perfect for debugging legacy projects or testing new releases.

  3. Easy Sharing

    • You can generate a requirements.txt or environment.yml.

    • Teammates can recreate your environment exactly.

  4. Avoid Conflicts

    • No more breaking project A by upgrading a package in project B.
  5. Cleaner Development Workflow

    • Your system remains clean.

    • Projects stay portable and reproducible.


Disadvantages (Let’s be honest)

  1. Learning Curve

    • Beginners often find the commands confusing at first.
  2. Extra Step

    • You always have to remember to “activate” the environment.
  3. Tool Fragmentation

    • Multiple tools: venv, conda, pipenv, poetry — which one should you use?

But trust me — once you get used to it, the benefits far outweigh the effort.


How to Use Virtual Environments

Let’s break it down with two popular methods: venv and conda.

Option 1: venv (Comes with Python)

Create an environment:

python -m venv myenv

Activate it:

  • On Windows:

      myenv\Scripts\activate
    
  • On macOS/Linux:

      source myenv/bin/activate
    

Install packages:

pip install numpy flask

Save dependencies:

pip freeze > requirements.txt

Exit environment:

deactivate

Option 2: conda (Anaconda/Miniconda users)

Create an environment:

conda create -n myenv python=3.11

Activate it:

conda activate myenv

Install packages:

conda install pandas matplotlib

Export config:

conda env export > environment.yml

Exit environment:

conda deactivate

What Does a Python Virtual Environment Look Like?

When you create a virtual environment in Python, it generates a folder that contains a self-contained Python installation along with its own installed packages and scripts. This folder isolates your project’s dependencies from your global Python setup.


Structure of a venv Environment (Standard Python)

If you create a virtual environment using the built-in venv module like this:

python -m venv myenv

It will create a folder named myenv that contains the following key components:

myenv/
├── Include/                   # Header files for compiling packages
├── Lib/
│   └── site-packages/         # Installed Python libraries for this env
├── Scripts/                   # Activation scripts and executables (Windows)
│   ├── activate.bat           # Script to activate this env on Windows
│   ├── python.exe             # Python executable for this environment
├── pyvenv.cfg                 # Configuration file describing this env
  • The Scripts folder holds the activation commands and the isolated Python executable.

  • The site-packages directory inside Lib is where all libraries you install with pip go.

  • pyvenv.cfg stores metadata about the environment.


Structure of a conda Environment

When you create an environment with Conda:

conda create -n myenv python=3.11

The environment is typically located under:

C:\Users\<YourUsername>\anaconda3\envs\myenv

And looks like this:

myenv/
├── DLLs/                     # Dynamic libraries
├── Library/                  # Libraries and binaries
├── Scripts/                  # Activation scripts and Python executables
│   ├── activate.bat
│   ├── conda.exe
│   ├── python.exe
├── conda-meta/               # Metadata about installed packages
├── etc/                      # Configuration files
├── python.exe                # Python executable for this environment
  • Conda environments are fully self-contained with their own Python interpreter and package libraries.

  • The conda-meta folder tracks package info for environment management.


Typical Project Setup with a Virtual Environment

A common setup for a Python project using a venv environment might look like this:

my-project/
├── env/                      # Virtual environment folder (created by venv)
├── app.py                    # Your application code
├── requirements.txt          # List of dependencies for easy sharing
└── README.md

Best Practices

  • Always create a virtual environment for each project.

  • Keep your requirements.txt or environment.yml versioned in Git.

  • Use .gitignore to avoid committing your environment folder.


Final Thoughts

A virtual environment might feel like a minor detail, but for a developer, it’s a cornerstone of clean, maintainable, and reproducible projects.

It gives you control.
It gives your code stability.
It makes you a better collaborator and a smarter developer.

0
Subscribe to my newsletter

Read articles from Bassem Shalaby directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Bassem Shalaby
Bassem Shalaby

For me, programming is a journey fueled by curiosity and creativity—an ongoing opportunity to learn, build, and bring ideas to life through thoughtful code. I'm sharing what I'm learning along the way—not as an expert, but as someone who enjoys the process and wants to grow. These are simply my thoughts and experiences from my learning journey. I genuinely appreciate any corrections or feedback if you notice something I’ve misunderstood or explained incorrectly. I'm here to learn, improve, and hopefully, one day, help others with the knowledge I’ve gained.