Back to Python Notes
Topic #225

Virtual Environments

By the end of this lesson, you will be able to create and activate a Python virtual environment to isolate project dependencies from your global system installation.

What it is

A virtual environment is an isolated directory containing its own Python interpreter and pip executable. It allows you to install packages for one project without affecting others or the global system. Think of it as a sandboxed workspace where each project has its own set of tools and libraries.

Related terms: venv (the standard library module), requirements.txt (a file listing dependencies), site-packages (where installed libraries live).

Why it matters

  • Dependency Isolation: Project A can use Django 4.0 while Project B uses Django 3.2 without conflict.
  • Reproducibility: You can share a requirements.txt file so teammates install the exact same versions.
  • Clean System: Prevents "dependency hell" by keeping global site-packages minimal.
  • Safety: Reduces the risk of breaking system tools that rely on specific Python package versions.

Syntax or steps

  1. Create: Run python -m venv <directory_name>. This generates a folder with the necessary structure.
  2. Activate: Source the activation script. This modifies your shell's PATH to prioritize the virtual environment's binaries.
  3. Install: Use pip install as usual; packages go into the local environment.
  4. Deactivate: Run deactivate to return to the global Python context.

Example

# Create a virtual environment named .venv in the current directory
python -m venv .venv

# Activate it (Linux/macOS)
source .venv/bin/activate

# Verify isolation: check which python is being used
which python

# Install a package locally
pip install requests

# List installed packages in this environment
pip list

# Deactivate when done
deactivate

Explanation: The first command builds the environment. The second command updates your terminal session so that typing python or pip points to the files inside .venv, not the system-wide ones. The which python command confirms this path change. Finally, deactivate restores your original shell configuration.

Common mistakes

  • Forgetting to activate: Installing packages while inactive puts them in the global scope. Always check your prompt for the environment name (e.g., (.venv)).
  • Committing the environment folder: Never add .venv to Git. Add it to .gitignore. Only commit requirements.txt.
  • Mixing Python versions: Ensure you run python -m venv using the correct Python version if multiple are installed (e.g., python3.11 -m venv .venv).
  • Hardcoding paths: Do not call .venv/bin/python directly in scripts unless necessary; rely on activation for cleaner code execution.

When to use it

Scenario Recommended Tool Reason
Standard Web/App Development venv Built-in, lightweight, sufficient for most projects.
Data Science / Complex Dependencies conda Handles non-Python binary dependencies (like C libraries) better.
Quick Script / One-off Task Global Install Overhead of creating an env may outweigh benefits for trivial tasks.

Practice

Guided Exercise: Create a new folder called test_project. Inside it, create a virtual environment named myenv. Activate it and install the flask package. Check that flask appears in pip list but does not appear when you deactivate and check again.

Challenge: Generate a requirements.txt file from your active environment using pip freeze > requirements.txt. Then, delete the myenv folder, recreate it, and reinstall all dependencies using pip install -r requirements.txt.

Quick check

Q: What happens if you run pip install numpy without activating your virtual environment?

A: NumPy will be installed in your global Python site-packages, potentially causing version conflicts with other projects that require different NumPy versions.

Summary

Virtual environments provide essential isolation for Python projects, ensuring that dependencies do not clash across different applications. By mastering venv creation, activation, and deactivation, you maintain a clean development workflow and reproducible builds.

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Virtual Environments – FAQs

Quick answers about learning Virtual Environments in Python.

This free note from Coding Now Tech Institute explains Virtual Environments in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Virtual Environments, is 100% free with no signup required.
With focused practice, most students grasp Virtual Environments in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now