On this page

Virtual Environments

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Why Virtual Environments Matter​

A virtual environment is an isolated Python workspace that contains its own interpreter and its own installed packages.

This isolation prevents conflicts between projects and ensures that each project uses the exact dependencies it requires.

Virtual environments solve critical problems:

Professional Python development always uses virtual environments.


How Virtual Environments Work​

A virtual environment is simply a directory containing:

When activated, the environment overrides the system Python so that:


Creating a Virtual Environment​

Use Python’s built‑in venv module.


python3 -m venv .venv

This creates a .venv directory inside your project.

.venv is a common, professional naming convention because:


Activating a Virtual Environment​

Activation modifies your shell so that the environment’s Python is used.

macOS / Linux​


source .venv/bin/activate

Windows (PowerShell)​


.venv\Scripts\Activate.ps1

Once activated:


Deactivating a Virtual Environment​

To return to the system Python:


deactivate

This restores your original shell environment.


Installing Packages Inside the Environment​

Once activated:


pip install requests

Packages are installed only inside .venv, not globally.

You can verify:


pip list

This shows only the environment’s packages.


Requirements Files for Reproducibility​

A requirements file records the exact package versions used in a project.

Generate it:


pip freeze > requirements.txt

Install from it:


pip install -r requirements.txt

This ensures:


Using Virtual Environments in IDEs​

Modern IDEs automatically detect .venv:

You can also manually select the interpreter if needed.


Best Practices​

These practices ensure clean, conflict‑free development environments.


Summary​

Virtual environments provide isolated, reproducible Python workspaces that prevent dependency conflicts and ensure professional‑grade project organization.

You learned how to create, activate, deactivate, and manage virtual environments, how to install packages safely, and how to maintain reproducibility with requirements files.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Typing & Type Hints

Next

Packaging & Distribution