Python Virtual Environments
Learn how to create isolated Python environments, manage project-specific dependencies and build a professional Python development workflow.
Learning Objectives
By the end of this lesson, you will understand why professional Python projects use isolated environments and how to create and manage them.
You will learn how to:
- Understand virtual environments.
- Understand dependency isolation.
- Create a
.venvenvironment. - Activate and deactivate environments.
- Install packages into an environment.
- Use
requirements.txt. - Recreate an environment.
- Use virtual environments with VS Code.
- Keep environments out of Git repositories.
- Troubleshoot common environment problems.
What Is a Virtual Environment?
A virtual environment is an isolated Python environment created specifically for a project.
It allows a project to have its own Python packages and package versions without affecting other projects on your computer.
python -m venv .venv
This creates a virtual environment named
.venv.
Simple Concept
Think of each virtual environment as a separate workspace for a Python project.
Why Use Virtual Environments?
Imagine you have two projects.
Project A
Requires one version of a package.
Project B
Requires a different version of the same package.
Installing everything globally can create dependency conflicts.
Virtual environments solve this by isolating the dependencies of each project.
Project A
└── .venv
└── packages for Project A
Project B
└── .venv
└── packages for Project B
Dependency Isolation
Suppose Project A needs:
requests==2.31.0
while Project B needs:
requests==2.32.3
Each project can maintain its own environment and dependency versions.
The Result
Updating a package in one project does not automatically change the dependency environment of another project.
Creating a Virtual Environment
First, open your terminal inside your project directory.
python -m venv .venv
The command uses Python's built-in
venv module.
You can also choose another name:
python -m venv myenvironment
However, .venv is a common convention for
project environments.
Understanding the Environment Structure
After creating an environment, your project may look approximately like this:
my-project/
│
├── .venv/
│
├── app.py
│
├── requirements.txt
│
└── README.md
The exact contents inside .venv vary by
operating system and Python version.
Do Not Edit .venv Manually
Treat the environment directory as generated infrastructure. Manage it through Python, pip and your development tools rather than manually editing its internal files.
Activating on Windows
In Windows Command Prompt:
.venv\Scripts\activate
In PowerShell:
.venv\Scripts\Activate.ps1
Once activated, your terminal usually displays the environment name.
(.venv) C:\Projects\my-project>
The (.venv) prefix is a useful visual
indication that the environment is active.
Activating on macOS and Linux
On macOS and Linux, use:
source .venv/bin/activate
After activation, the terminal commonly displays:
(.venv)
The activation command differs because operating systems use different executable and shell layouts.
Verifying the Environment
After activation, check which Python interpreter is being used.
python --version
You can also inspect the Python executable path.
On Windows:
where python
On macOS/Linux:
which python
Why Verify?
If Python points to your virtual environment, your project commands are using the intended isolated environment.
Installing Packages Inside the Environment
Once the environment is active, install packages normally.
python -m pip install requests
The package is installed into the active environment.
python -m pip list
This allows your project to maintain its own dependencies.
Professional Workflow
Activate environment
↓
Install dependencies
↓
Develop application
↓
Test application
↓
Record dependencies
Deactivating the Environment
When you finish working with the environment, use:
deactivate
The (.venv) indicator should disappear from
your terminal.
Important
Deactivating does not delete the environment. It simply switches your current shell back to its normal Python context.
Virtual Environments and requirements.txt
Virtual environments isolate packages, while
requirements.txt records which packages
the project needs.
python -m pip freeze > requirements.txt
A requirements file might contain:
requests==2.32.3
pandas==2.2.3
numpy==2.1.3
Another developer can then install the dependencies with:
python -m pip install -r requirements.txt
Recreating an Environment
One of the biggest advantages of this workflow is that environments can be recreated.
Suppose your original environment is deleted.
Create a new one:
python -m venv .venv
Activate it and install dependencies:
python -m pip install -r requirements.txt
Key Concept
You normally do not need to copy the entire
.venv directory between computers.
Recreate the environment and install the project's
dependencies instead.
Using Multiple Environments
A developer can have many projects, each with its own environment.
Projects/
│
├── ecommerce/
│ ├── .venv/
│ └── app.py
│
├── analytics/
│ ├── .venv/
│ └── analysis.py
│
└── website/
├── .venv/
└── app.py
Each project can maintain its own dependency set.
Virtual Environments in VS Code
VS Code can detect Python environments and allow you to choose which interpreter your project should use.
After creating:
python -m venv .venv
Open the project in VS Code and select the Python
interpreter associated with .venv.
Why This Matters
Your editor, terminal, debugger and Python execution should ideally use the same environment.
If VS Code uses a different interpreter from your terminal, you may see confusing import or package errors.
Virtual Environments and Git
Virtual environments usually should not be committed to your Git repository.
Instead, developers normally commit the project's source code and dependency information.
.venv/
Add it to .gitignore:
.venv/
Recommended Project Structure
project/
│
├── .venv/ ← local only
├── .gitignore
├── app.py
├── requirements.txt
└── README.md
Common Mistakes
1. Installing Packages Before Activation
You may accidentally install packages into another environment or the global Python installation.
2. Wrong Python Interpreter
Your editor may be using a different interpreter from your terminal.
3. Committing .venv to Git
The environment directory can be large and is normally unnecessary in source control.
4. Copying .venv to Another Computer
Recreating the environment from dependency information is generally more reliable.
5. Forgetting Which Environment Is Active
Always check the terminal prompt and interpreter path when something behaves unexpectedly.
Debugging Tip
When you see
ModuleNotFoundError, one of the first
things to check is whether the package was installed
into the same Python environment that is running
your program.
Practice Exercises
Exercise 1 — Create an Environment
Create a project folder and create a
.venv environment inside it.
python -m venv .venv
Exercise 2 — Activate It
Activate your environment using the command appropriate for your operating system.
Exercise 3 — Install a Package
Install requests inside the environment.
Exercise 4 — Verify
Check the installed packages and Python executable.
Exercise 5 — Requirements
Generate a requirements.txt file using
pip freeze.
Mini Challenge — Build a Reproducible Project
Create a small Python project that can be recreated on another computer.
Your Mission
-
Create a folder named
sales-analyzer. -
Create a
.venvenvironment. - Activate it.
-
Install
pandas. -
Create a Python file named
analysis.py. -
Create
requirements.txt. -
Add
.venv/to.gitignore.
Suggested Structure
sales-analyzer/
│
├── .venv/
├── .gitignore
├── analysis.py
├── requirements.txt
└── README.md
Suggested Commands
python -m venv .venv
python -m pip install pandas
python -m pip freeze > requirements.txt
Challenge Extension
Delete the .venv folder, recreate it
and restore the dependencies using
requirements.txt.
Knowledge Check
1. What is a virtual environment?
2. Which command creates a virtual environment?
3. Why are virtual environments useful?
4. Which command deactivates an environment?
5. What file commonly records dependencies?
6. Should .venv normally be committed to Git?
7. Which command installs requirements?
8. What does .venv commonly represent?
Lesson Summary
Virtual environments are a fundamental part of professional Python development. They allow each project to maintain isolated dependencies.
What You Learned
- A virtual environment isolates Python dependencies.
-
python -m venv .venvcreates one. - Activation makes the environment the current project context.
- Packages installed while active belong to that environment.
-
deactivateexits the environment. -
requirements.txtrecords project dependencies. - Environments can be recreated instead of copied between computers.
-
.venvshould normally be excluded from Git repositories. - VS Code should use the same Python interpreter as the project's environment.
You now understand one of the most important professional Python workflows: environment isolation + dependency management.
Next, we will learn how to find and fix problems in Python programs through systematic debugging.
Ready to Continue?
You have completed the Python Virtual Environments lesson.