Virtual Environments (venv)
Learn why virtual environments exist, how to create and activate one with venv, and why project dependencies should never be installed globally.
Introduction
Imagine two different projects on your computer: one needs version 1.0 of a library, and another needs version 3.0 of the exact same library. If Python only had one global place to install packages, you would not be able to satisfy both projects at the same time. Virtual environments solve exactly this problem.
In this lesson, you will learn why virtual environments matter, how to create and activate one using Python's built-in venv module, how to tell when one is active, and why professional Python projects almost always use them.
- Why virtual environments exist and what problem they solve.
- How to create a virtual environment with python -m venv venv.
- How to activate a venv on Windows and on macOS/Linux.
- How to tell whether a venv is currently active.
- Why you should avoid installing project dependencies globally.
Why Virtual Environments Exist
Every Python installation has one global set of installed packages by default. If every project shared that same global set, you would quickly run into conflicts: Project A needs requests version 2.10, Project B needs requests version 2.31, and installing one would break the other.
A virtual environment (often shortened to "venv") is an isolated, self-contained folder that holds its own Python interpreter copy and its own separate set of installed packages, completely independent from your system's global Python installation and from any other project's virtual environment.
Isolation
Each project gets its own independent set of installed packages.
No Version Conflicts
Two projects can use different versions of the same package without interfering with each other.
Clean Global Python
Your system-wide Python installation stays untouched and predictable.
Reproducibility
A venv makes it easy to record and recreate the exact dependencies a project needs (covered in the next lesson).
Creating a Virtual Environment
Python includes a built-in module called venv specifically for creating virtual environments — no separate installation needed. Run this command inside your project folder.
python -m venv venvHere, python -m venv runs the venv module, and the second venv is simply the name of the folder that will be created to hold the environment. You can name it anything, but venv (or .venv) is the most common convention.
my_project/
venv/
Scripts/
Lib/
pyvenv.cfg
main.pymy_project/
venv/
bin/
lib/
pyvenv.cfg
main.pyIt is standard practice to create a fresh virtual environment for each individual project, rather than sharing one venv across multiple unrelated projects.
Activating a Virtual Environment
Creating a venv does not automatically use it — you must activate it first. The activation command differs depending on your operating system and shell.
Windows (Command Prompt)
venv\Scripts\activate
Windows (PowerShell)
venv\Scripts\Activate.ps1
macOS / Linux
source venv/bin/activate
venv\Scripts\activatesource venv/bin/activateOn Windows PowerShell, activation may be blocked by the default script execution policy. If so, you may need to allow local scripts (e.g., Set-ExecutionPolicy -Scope Process RemoteSigned) before activation will run.
How to Tell a venv is Active
Once activated, your terminal prompt changes to show the environment's name in parentheses at the start of the line. This is the quickest visual confirmation that the virtual environment is active.
C:\my_project>(venv) C:\my_project>You can also confirm which Python interpreter is currently in use — it should now point inside the venv folder rather than your system Python.
which python/home/user/my_project/venv/bin/pythonDeactivating a Virtual Environment
When you are done working, or want to switch back to the global Python installation, simply run the deactivate command — it works the same way on every operating system.
deactivateThe (venv) prefix disappears from your prompt, confirming you are back to using the system-wide Python environment.
Why Not Install Globally?
It can feel like an extra, unnecessary step to create and activate a venv for every project — but skipping it causes real problems as soon as you work on more than one project.
Installing Globally
- All projects share one set of package versions.
- Upgrading a package for one project can silently break another.
- Hard to know exactly what a specific project actually depends on.
- Risk of needing administrator/root permissions to install packages.
Using a Virtual Environment
- Each project has its own isolated, independent dependencies.
- Safe to use different versions of the same package across projects.
- Dependencies are easy to record and reproduce (via requirements.txt).
- No special permissions needed — the venv folder is just part of your project.
Never install project-specific packages into your global/system Python. Always create and activate a virtual environment first, and install dependencies inside it.
Common Mistakes
- Installing packages before activating the virtual environment (they go to the global Python instead).
- Forgetting to activate the venv in a new terminal window or session.
- Committing the entire venv folder to version control (it should be excluded, e.g., via .gitignore).
- Sharing one venv across multiple unrelated projects.
- Confusing "creating" a venv with "activating" it — both steps are required.
Best Practices
- Create a new virtual environment for every new project.
- Activate the venv every time before installing packages or running the project.
- Add the venv folder to .gitignore so it is never committed to version control.
- Double-check your prompt shows the (venv) prefix before installing dependencies.
- Deactivate when switching to a different project or task.
Frequently Asked Questions
Do I need to install venv separately?
No. The venv module has been part of Python's standard library since Python 3.3, so it is always available with a normal Python installation.
Can I name my virtual environment folder something other than venv?
Yes, you can name it anything (e.g., .venv or env), but venv and .venv are the most widely recognized conventions.
Do I need a different activation command on every operating system?
Yes. Windows uses venv\Scripts\activate (or Activate.ps1 in PowerShell), while macOS and Linux use source venv/bin/activate.
What happens to my installed packages if I delete the venv folder?
They are deleted along with it, since they live entirely inside that folder. This is actually a benefit — you can always recreate a clean environment from scratch.
Should the venv folder be included in a project shared with others?
No. It should be excluded from version control. Instead, share a requirements.txt file (covered in the next lesson) so others can recreate the same environment themselves.
Key Takeaways
- Virtual environments isolate each project's dependencies from the global Python installation and from each other.
- Create one with python -m venv venv inside your project folder.
- Activation differs by OS: venv\Scripts\activate on Windows, source venv/bin/activate on macOS/Linux.
- An active venv is shown by a (venv) prefix in your terminal prompt.
- Never install project dependencies into your global Python — always work inside an activated venv.
Summary
Virtual environments keep each Python project's dependencies isolated, avoiding version conflicts and keeping your global Python installation clean. Creating one is a single command, and activating it is just as simple, though the exact command depends on your operating system.
With a solid understanding of why and how to use venv, you are ready to learn how to actually install and manage packages inside that environment using pip, in the next lesson.
- You understand why virtual environments are essential for real projects.
- You can create a virtual environment with python -m venv venv.
- You can activate and deactivate a venv on any operating system.
- You know never to install project dependencies globally.