Python Course
Virtual Environments
Every Python project you build will eventually depend on external packages — web frameworks, data libraries, HTTP clients, and more. Without a system to manage those dependencies, packages from different projects collide, version conflicts break things silently, and sharing your project with others becomes a guessing game. Virtual environments solve all of this by giving each project its own isolated Python installation with its own packages.
This lesson covers creating, activating, and managing virtual environments, working with pip, and the professional workflow used on every real-world Python project.
The Problem Virtual Environments Solve
Imagine you have two projects on the same machine:
- Project A needs
requests==2.28.0 - Project B needs
requests==2.31.0
Without virtual environments, only one version of requests can be installed globally at a time. Installing for one project silently breaks the other. Virtual environments eliminate this by creating a completely separate package space for each project — installing into one environment has zero effect on any other.
Creating a Virtual Environment
Python ships with the venv module built in — no installation required. One command creates the environment.
Why it exists: venv creates a self-contained directory with its own Python interpreter, pip, and site-packages folder. Anything installed inside it stays inside it.
# Run these commands in your terminal — not in Python
# Step 1 — Create a virtual environment named "venv"
# python -m venv venv
# The venv/ folder structure looks like this:
# venv/
# ├── bin/ (Mac/Linux) or Scripts/ (Windows)
# │ ├── python
# │ ├── pip
# │ └── activate
# ├── lib/
# │ └── python3.x/
# │ └── site-packages/ ← your packages install here
# └── pyvenv.cfg- The name
venvis a convention — you can name it anything, butvenvor.venvare most common - Use
.venv(with a dot) to hide it from directory listings on Mac and Linux - The environment folder should never be committed to version control — add it to
.gitignore python -m venv venvuses whicheverpythonis on your PATH — usepython3on Mac/Linux if needed
Activating and Deactivating
Creating the environment does not automatically use it. You must activate it first. Activation modifies your terminal session so that python and pip point to the environment's versions instead of the system ones.
# Activation commands — run in terminal
# Mac / Linux:
# source venv/bin/activate
# Windows (Command Prompt):
# venv\Scripts\activate.bat
# Windows (PowerShell):
# venv\Scripts\Activate.ps1
# After activation your prompt changes to show the environment name:
# (venv) $
# Verify you are using the environment's Python
# which python ← Mac/Linux
# where python ← Windows
# python --version
# Deactivate — return to the system Python
# deactivate- When activated,
pythonandpiprefer exclusively to the environment — not the system - The
(venv)prefix in your terminal prompt confirms the environment is active deactivatereturns you to the global Python — it never deletes anything- You must activate the environment every time you open a new terminal session
Installing Packages with pip
With the environment active, pip install installs packages exclusively into that environment. Nothing touches the system Python or any other project.
Real-world use: every data science project installs pandas, numpy, and matplotlib into its own environment so version changes for one project never affect another.
# pip commands — run in terminal with environment active
# Install a single package
# pip install requests
# Install a specific version
# pip install requests==2.31.0
# Install multiple packages at once
# pip install flask pandas numpy
# Upgrade a package to its latest version
# pip install --upgrade requests
# Uninstall a package
# pip uninstall requests
# List all installed packages and their versions
# pip list
# Show details about one specific package
# pip show requests- Always activate the environment before running
pip install— otherwise packages go into the system Python pip install package==versionpins an exact version — critical for reproducibilitypip listshows every package installed in the currently active environmentpip show packagedisplays version, location, dependencies, and homepage
Freezing Dependencies — requirements.txt
A requirements.txt file lists every package and exact version your project needs. It is the standard way to share a project's dependencies so anyone can recreate the same environment exactly.
Why it exists: without a requirements file, a teammate cloning your project has no way to know which packages to install or which versions you used. A requirements file makes setup a single command.
Real-world use: every production Python project — web apps, data pipelines, ML models — ships with a requirements.txt so the deployment server installs exactly the same packages as the developer's machine.
# requirements.txt workflow — run in terminal
# Step 1 — Freeze current environment to a file
# pip freeze > requirements.txt
# The file looks like this:
# certifi==2024.2.2
# charset-normalizer==3.3.2
# idna==3.6
# requests==2.31.0
# urllib3==2.2.1
# Step 2 — Someone else recreates the exact environment
# python -m venv venv
# source venv/bin/activate (or Scripts\activate on Windows)
# pip install -r requirements.txtpip freezeoutputs every installed package and its exact version to stdout> requirements.txtredirects that output into a filepip install -r requirements.txtinstalls every package listed in the file- Re-run
pip freeze > requirements.txtevery time you add or upgrade a package
The Complete Project Workflow
Putting it all together — this is the exact sequence every professional Python developer follows when starting or joining a project.
# Full professional workflow — terminal commands
# --- Starting a new project ---
# mkdir my_project && cd my_project # create project folder
# python -m venv venv # create virtual environment
# source venv/bin/activate # activate it (Mac/Linux)
# pip install requests flask # install what you need
# pip freeze > requirements.txt # save dependency list
# echo "venv/" >> .gitignore # never commit the env folder
# --- Joining an existing project ---
# git clone https://github.com/org/project.git
# cd project
# python -m venv venv # create a fresh environment
# source venv/bin/activate # activate it
# pip install -r requirements.txt # install exact dependencies- The environment folder (
venv/) is always in.gitignore— it is never pushed to a repository requirements.txtIS committed — it is the contract that defines the project's dependencies- This workflow works identically on every OS — only the activation command differs
Using venv in VS Code and PyCharm
Both major Python IDEs detect and integrate virtual environments automatically, so you rarely need to activate from the terminal once the environment is set up.
- VS Code: open the Command Palette (
Ctrl+Shift+P), search Python: Select Interpreter, and choose thevenvinterpreter from the list. VS Code activates it automatically in every new terminal it opens. - PyCharm: go to Settings → Project → Python Interpreter, click the gear icon, choose Add Interpreter → Virtualenv Environment, and point it at your existing
venvfolder. PyCharm handles activation transparently. - Both IDEs show the active environment name in the status bar at the bottom of the window.
pip vs conda — A Quick Comparison
You may also encounter conda, a different package and environment manager popular in data science. Here is how they compare so you know when to use which.
| Feature | pip + venv | conda |
|---|---|---|
| Package source | PyPI (Python packages only) | Conda channels (Python + C libs) |
| Built into Python | Yes | No — requires Anaconda/Miniconda |
| Best for | Web dev, general Python projects | Data science, ML, scientific computing |
| Industry standard | Most common overall | Common in data/ML teams |
Practice Questions
Practice 1. What command creates a virtual environment named venv in the current directory?
Practice 2. What command saves all currently installed packages and their exact versions to a file?
Practice 3. What command installs all packages listed in a requirements.txt file?
Practice 4. Should the venv/ folder be committed to a Git repository?
Practice 5. What terminal command deactivates an active virtual environment?
Quiz
Quiz 1. What is the main purpose of a virtual environment?
Quiz 2. What happens when you run pip install requests without activating a virtual environment first?
Quiz 3. What does the (venv) prefix in the terminal prompt indicate?
Quiz 4. Which file should be committed to version control to share project dependencies?
Quiz 5. Which module built into Python is used to create virtual environments?
Next up — Python Libraries Overview: a tour of the most important standard library and third-party packages every Python developer should know.