This article explains how Python virtual environments work internally
and how to use them in practice.

Where Python Packages Are Installed

You can inspect Python's package locations using the site module.

import site
site.getsitepackages()

This returns a list of absolute paths where system-level Python
packages
are installed.

Without a virtual environment:

  • pip install <package> installs packages globally.
  • Packages are stored in:
<!-- -->
<python-installation>/lib/site-packages

This location is relative to the Python executable.

Problem

Different projects often require:

  • Different dependencies
  • Different versions of the same dependency

Installing everything globally causes version conflicts.

Solution

Virtual environments isolate dependencies by changing the
site-packages directory used by Python.

Each environment has its own package directory.


What Creating a Virtual Environment Actually Does

Creating a virtual environment mainly changes where Python installs
and loads packages from
.

Environment Creation

Running a virtual environment command creates a directory, for example:

myenv/

Inside the directory:

myenv/
├── bin/
│   ├── activate
│   └── python -> system python
├── include/
└── lib/
    └── pythonX.X/
        └── site-packages/

Key points:

  • bin/python is usually a symbolic link to the system Python
    interpreter.
  • Python installs packages relative to the interpreter path:
<!-- -->
../lib/site-packages

So packages installed inside the environment go to:

myenv/lib/pythonX.X/site-packages

What Activation Does

Activating an environment modifies the PATH environment variable.

Example path added:

/var/www/projectX/myenv/bin

This path is prepended to PATH.

Result:

  • Running python executes:
<!-- -->
myenv/bin/python

instead of the system Python.

Effect on Package Installation

After activation:

pip install <package>

installs packages into:

myenv/lib/site-packages

This keeps dependencies isolated per project.


Creating Virtual Environments

Python 2.x

Python 2 requires the virtualenv package.

Install it first:

pip install virtualenv

Create an environment:

virtualenv env

env is simply the folder name.


Python 3.x

Python 3 includes the venv module.

Create an environment using:

python3 -m venv env

Version Control Considerations

If the virtual environment is inside the project directory, ignore it in
version control.

Example .gitignore entry:

/env

Instead of committing the environment, track dependencies using a
requirements file.

Example:

pip freeze > requirements.txt

Virtual Environment Folder Structure

Typical layout:

env/
├── bin
│   ├── activate
│   └── python
├── include
└── lib
    └── pythonX.X
        └── site-packages

Important components:

bin/activate

Activation script.

Responsibilities:

  • Modifies PATH
  • Ensures the environment's Python interpreter is used.

lib/site-packages

Stores all installed project dependencies.

Any package installed after activation is placed here.


Standard Workflow

Typical usage pattern:

  1. Ensure Python and pip are installed.
  2. Navigate to the location where the environment should live.
  3. Create the environment.
  4. Activate it.
  5. Install project dependencies.

Example:

python3 -m venv env
source env/bin/activate
pip install -r requirements.txt

Tools Built on Top of Virtualenv

Several tools simplify environment management.

virtualenvwrapper

Adds commands such as:

  • mkvirtualenv
  • workon
  • deactivate
  • cdvirtualenv

Requires initial setup.


pipenv

Combines:

  • pip
  • virtualenv

Features:

  • Automatically creates a virtual environment
  • Uses a hashed environment name
  • Tracks dependencies in project files

Example workflow:

pipenv install <package>
pipenv shell

pipenv manages both dependency resolution and en