Skip to content

Virtual Environments & Dependency Management

In professional software engineering, isolation is safety. A “Virtual Environment” is a self-contained directory tree that contains a specific version of the Python interpreter and its own set of installed libraries.

Mastering virtual environments is the difference between a hobbyist and a professional developer.


Imagine you have two projects on your computer:

  • Project A (Legacy): Requires Django version 2.2.
  • Project B (Modern): Requires Django version 5.0.

If you install Django globally (pip install django), you can only have one version. Upgrading for Project B will break Project A. This is known as “Dependency Hell.”

By creating a virtual environment for every project, you ensure that Project A and Project B have their own private “drawer” for libraries, preventing they from ever seeing or interfering with each other.


Navigate to your project folder and run the venv module. We typically name the folder .venv (the dot makes it hidden in many systems).

Terminal window
# Windows / Mac / Linux
python -m venv .venv

You must “activate” the environment to tell your current terminal session to use the Python interpreter inside the .venv folder instead of the global one.

Terminal window
.venv\Scripts\Activate.ps1

How do I know it worked? Your terminal prompt will usually be prefixed with (.venv). You can also run which python (Unix) or where python (Windows) to verify the path points to your project folder.


Once the environment is active, you use pip (the Package Installer for Python) to manage your libraries.

Terminal window
pip install requests

In a professional setting, you don’t just tell coworkers to “install the libraries.” You provide a manifest.

  1. Generate the manifest:

    Terminal window
    pip freeze > requirements.txt

    This creates a file listing every library and its exact version (e.g., requests==2.31.0).

  2. Install from a manifest: When a teammate downloads your project, they can recreate your exact environment with one command:

    Terminal window
    pip install -r requirements.txt

What actually happens when you activate a virtual environment? It’s surprisingly simple: Environment Variables.

  1. The PATH Variable: When you run activate, Python modifies your system’s PATH variable, putting the .venv/bin (or Scripts) directory at the very beginning.
  2. The Search Order: When you type python, the operating system looks at the PATH, finds the version in your .venv folder first, and stops searching.
  3. Site-Packages: The Python interpreter inside the .venv is configured to look for libraries only within its own lib/pythonX.X/site-packages folder, completely ignoring your global libraries.

  • One Project, One Environment: Never share environments between different projects.
  • Never Commit .venv: Do not upload your environment folder to GitHub. It is machine-specific and huge.
  • Use .gitignore: Always add .venv/ to your .gitignore file.
  • Always Activate: Before running or testing your code, ensure your environment is active to avoid “ModuleNotFoundError” or using the wrong version of a library.