Python Virtual Environments (venv) Explained
A Python venv keeps each project's packages separate so projects cannot break each other. Learn the exact commands for macOS, Linux and Windows, plus requirements.txt.
Short answer: A Python virtual environment (venv) is a separate folder that holds its own copy of installed packages for one project, so different projects cannot break each other's libraries. You create one with python -m venv .venv, activate it, then use pip install to add packages only to that project.
Python venv explained: why do you need one?
A Python venv solves a simple problem. Without one, every pip install goes into one shared place on your computer. Imagine project A needs an older version of a library and project B needs a newer one. Installing for B silently upgrades A, and A stops working. With a venv per project, each project keeps its own versions.
There are two more reasons. First, on some Linux systems pip refuses to install into the system Python and suggests a virtual environment instead, because the operating system relies on its own packages. Second, a venv makes a project easy to share: you can list exactly what it needs.
How do you create and activate a venv?
Open a terminal in your project folder and run these commands. On macOS and Linux, type python3 instead of python if python is not found.
# 1. Create the environment (once per project, inside the project folder)
python -m venv .venv
# 2. Activate it (every time you open a new terminal)
source .venv/bin/activate # macOS / Linux
.venv\Scripts\activate # Windows
# 3. Install packages into it
pip install requests
# 4. Save the exact versions you use
pip freeze > requirements.txt
# 5. Leave the environment
deactivateTask | macOS / Linux | Windows |
|---|---|---|
Create |
|
|
Activate |
|
|
Install a package |
|
|
Save versions |
|
|
Leave |
|
|
The name .venv is only a convention; it is just a folder name. If PowerShell on Windows refuses to run the activation script, the official Python venv documentation explains the execution policy setting.
How do you know the venv is active?
When a venv is active, most terminals show its name in brackets at the start of the prompt, such as (.venv). For a definite answer, save this as check_env.py and run python check_env.py:
import sys
print("Python running from:", sys.prefix)
print("Inside a virtual environment:", sys.prefix != sys.base_prefix)Inside an active venv the output looks like this (your path will differ):
Python running from: /home/sam/weather-app/.venv
Inside a virtual environment: TrueIf the second line says False, you are using the global Python and anything you install goes there. This small check also explains many ModuleNotFoundError messages: the package was installed in one environment and the script ran in another. More such cases are in common Python errors and how to fix them.
How do you share a project with requirements.txt?
A requirements.txt file is the shopping list for a venv. pip freeze > requirements.txt writes every installed package and its exact version into the file. Someone else, or you on another computer, rebuilds the same environment with:
python -m venv .venv
source .venv/bin/activate # or .venv\Scripts\activate on Windows
pip install -r requirements.txtShare the requirements.txt file, not the .venv folder. Add .venv/ to your .gitignore so Git never uploads it; the folder is large and tied to your machine.
Is venv the same as conda or other tools?
Tool | What it is | Good fit |
|---|---|---|
venv | Built into Python 3; isolates packages per project | Learning, most small and medium projects |
conda | A separate package and environment manager that can also install Python itself and non-Python libraries | Data science setups with complex native libraries |
Poetry, uv and similar | Tools that create environments and also manage dependencies and lock files | Teams that want stricter, repeatable setups |
All of them share the same idea: one isolated environment per project. Learning venv first makes the others easy to understand. The free chapter Setting up Python, VS Code and venv walks through the full setup, and modules, packages and pip goes deeper into installing.
Common mistakes
Forgetting to activate the venv in a new terminal, then installing packages globally.
Committing the
.venvfolder to Git.Moving or renaming the venv folder; it stores fixed paths, so delete it and create a new one instead.
Running a pip that belongs to a different Python.
python -m pip install ...always uses the Python you name.Your editor using a different interpreter from your terminal. In VS Code, select the interpreter inside
.venv.
Frequently Asked Questions
Should I name the folder .venv or venv?
Either works, because the name is only a convention. Many people use .venv because editors often detect it automatically and the dot keeps it out of the way in file listings.
How do I delete a virtual environment?
Run deactivate if it is active, then delete the .venv folder. Nothing else on your computer depends on it, and you can recreate it from requirements.txt at any time.
Can a venv use a different Python version?
No. A venv uses the Python version that created it. To use another version, install that Python first and create the venv with it.
Do I need a venv in Google Colab?
Usually not. Google Colab runs each notebook session on its own temporary machine, so you normally install extra packages there directly with pip.
Key Takeaway
A venv gives each Python project its own packages, so projects do not break each other. Create it with python -m venv .venv, activate it, install with pip, save versions to requirements.txt, and keep the folder out of Git.
Ready to start? You will set up Python properly and build real projects, from your first script to your first AI app. Begin with the free course Python for AI: From Zero to Your First AI App.
Related articles
More in Python
What Is scikit-learn? The First Machine Learning Library to Learn
scikit-learn is the standard Python library for classic machine learning. See what it does, how to install it, and a complete first model in a few lines.
4 min readNov 27, 2026Web Scraping With Python: A Beginner's Guide
Learn the basics of web scraping with Python using requests and BeautifulSoup, and the rules that keep you polite and safe: robots.txt, terms of service, rate limits and personal data.
4 min readNov 24, 2026Clean CSV Data With Pandas: 10 Useful Commands
Ten pandas commands that fix most messy CSV files: spaces, capitals, duplicates, wrong types and missing values, shown in one example you can run.
4 min readNov 21, 2026TensorFlow vs PyTorch: Which Should Beginners Learn?
TensorFlow and PyTorch teach the same core ideas. Compare them for beginners, see what they automate with a small runnable Python example, and learn how to choose without wasting weeks.
4 min readNov 19, 2026Jupyter vs VS Code vs Google Colab
Where should you write your first Python code? Compare Jupyter Notebook, VS Code and Google Colab, with a simple rule for choosing.
4 min readNov 15, 2026Your First Python Project: Build a Calculator
Build a command-line calculator in about 30 lines of Python, learning functions, loops, conditions and error handling along the way.
3 min readNov 12, 2026
Comments
Sign in to join the discussion.

