Sub topic 1
Why projects need their own environments
Prerequisites
This chapter assumes you can run commands in a terminal and that Python 3 is installed and available on the command line. You should be able to run a script and import a module. You do not need to know venv or pip yet. This chapter explains the problem they solve. Later chapters cover the commands in detail.
The problem: one Python, many projects
A Python installation has one place where third-party packages go. By default, every project that uses that interpreter sees the same packages at the same versions. This works until two projects disagree.
Suppose you maintain two projects:
- A reporting script written two years ago. It depends on
requests2.20 and a helper library that breaks on newer releases. - A new web service. It needs
requests2.31 or later because it relies on a fix added in that release.
If both projects share one installation, only one version of requests can be present at a time. Installing the version one project needs breaks the other. This is a dependency conflict, and it is the main reason isolated environments exist.
Shared installations cause other problems too:
- Hidden dependencies. A package you installed for an old project is still importable in a new one. Your code may work on your machine only because of that leftover package. A teammate who lacks it will see an
ImportError. - Transitive conflicts. Packages depend on other packages. Two libraries can each require a different range of a third library, and the ranges may not overlap.
- System breakage. On many Linux distributions and on macOS, the system's own tools use the system Python. Upgrading a package there can break those tools. Recent distributions mark the system Python as "externally managed" and make
piprefuse to install into it for this reason. - Permission problems. Installing into a system location needs administrator rights. Using
sudo pip installis a common source of broken systems.
What an environment gives you
A virtual environment is a directory that holds its own copy of (or link to) a Python interpreter and its own set of installed packages. When you run Python from that environment, it looks for third-party packages in the environment, not in the base installation.
The result is isolation along one axis: installed packages. Each project gets its own set, at its own versions, without affecting the others.
The diagram shows two projects that share one base Python installation but keep separate package sets.
Both environments reuse the base installation's interpreter and standard library. Only the third-party packages differ.
What isolation does not cover
It is easy to overestimate what a virtual environment does. It isolates Python packages. It does not isolate:
- The Python version itself. An environment is built from one existing interpreter. A Python 3.10 environment stays on 3.10. To use another version, you need that version installed and you create a new environment from it.
- System libraries. If a package links against a C library such as
libpqorlibxml2, that library comes from the operating system and is shared. - Files, network access, or processes. An environment is not a sandbox, a container, or a virtual machine. Code running in it has the same permissions as you.
- Environment variables and the rest of your shell, apart from what activation changes.
Keeping this boundary clear helps with troubleshooting later. A problem caused by a system library will not be fixed by recreating the environment.
How Python finds packages
Python builds a list of directories, sys.path, and searches it in order when you run import. One entry is the site-packages directory, where pip puts installed packages.
For the base installation, site-packages lives inside the Python installation or a per-user location. For a virtual environment, Python finds a pyvenv.cfg file next to the interpreter and uses the environment's own site-packages instead. Later chapters explain this in detail. For now, the useful idea is that the environment changes which directory import searches.
You can see the effect with a short script. Save it as where.py:
import sys
print("Interpreter:", sys.executable)
print("Environment prefix:", sys.prefix)
print("Base prefix:", sys.base_prefix)
print("In a virtual environment:", sys.prefix != sys.base_prefix)
for entry in sys.path:
print(" path entry:", entry)Run it with the base interpreter and sys.prefix equals sys.base_prefix. Run it with an environment's interpreter and the two differ, and the site-packages entry in sys.path points inside the environment. You will use this check often.
A demonstration of the conflict
The following session shows the problem without an environment. It uses a throwaway directory so nothing important is touched, and it assumes you run it only where installing into the base interpreter is allowed. If your system blocks this, skip to the next section, since the point is only to show the idea.
python3 -m pip install "requests==2.20.0"
python3 -m pip show requests
python3 -m pip install "requests==2.31.0"
python3 -m pip show requestsThe second install replaces the first. Both show commands report one version at a time, and the first version is gone. Nothing warns you that a project somewhere else relied on it. If you had two projects, you would have to reinstall the right version each time you switched.
With separate environments, each project keeps its own version, and switching is a matter of choosing which environment's interpreter to run.
Reproducibility and teamwork
Isolation helps in a second way. When a project has its own environment, the set of packages in it is exactly what the project needs. That set is easy to list and share. A teammate can recreate it on another machine and get the same behavior.
Without isolation, the list of installed packages contains everything you ever installed. A record of it is noisy, and it may hide the fact that your project needs a package you forgot to declare.
This is why the later chapters on requirements files and constraints files depend on this one. A requirements file is only useful if it describes one project, and a per-project environment is what makes that true.
Environments are disposable
A virtual environment should be cheap to delete and recreate. You should not store anything in it that you cannot rebuild. Your own source code belongs outside it, in the project directory. The environment holds only installed packages and a few helper files.
This has practical consequences:
- If an environment breaks, delete it and build a new one from your requirements file.
- Do not commit the environment to version control. Commit the file that describes it.
- Do not move or copy an environment to another directory or machine. It contains absolute paths, and it will not work reliably. Recreate it instead.
A later chapter covers keeping environments out of version control and rebuilding them.
Alternatives and where they fit
Several approaches address the same problem, with different trade-offs.
| Approach | Isolates | Main cost |
|---|---|---|
| Shared base installation | Nothing | Conflicts and hidden dependencies |
| `venv` environment | Python packages | Must create and activate one per project |
| Container or virtual machine | Packages, system libraries, and the operating system | Heavier setup and more resources |
| `--user` installs | Packages from the system location only | All your projects still share one set |
For most day-to-day Python work, one venv environment per project is the right balance. Containers are a good addition when system libraries or the operating system also need to match across machines. They do not replace the need to manage Python packages inside the container, and many teams use a virtual environment there as well.
The final chapter of this tutorial covers virtualenv, pipx, and uv, which build on the same ideas.
Common mistakes at this stage
- Installing into the base interpreter by habit. If you type
pip installwithout checking which Python it belongs to, you may change the shared installation. The next chapters show how to avoid this. - Assuming a project is isolated because it "works". A script can run correctly only because an unrelated package happens to be installed. Test in a fresh environment to find out.
- Treating the environment as part of the project. Your project is the source code and the list of dependencies. The environment is a build product.
- Expecting an environment to change the Python version. It cannot. Choose the interpreter when you create it.
Summary
Python installs packages into one shared location by default, so projects with different needs collide. A virtual environment gives each project its own package set on top of a shared base interpreter. It isolates installed packages only, and it is disposable by design. The next chapter shows how to create one with python -m venv.
Exercise
Two projects need different major versions of the same library. Name two problems you would face if both used the base Python installation, and explain why a virtual environment per project solves the main one.
Show answer
First, only one version of the library can be installed at a time, so installing the version for one project breaks the other. Second, any package you installed for one project stays importable in the other, which can hide missing dependency declarations (system breakage or permission problems are also valid answers). A virtual environment per project gives each its own site-packages directory, so each project can hold its own version of the library without affecting the other.
Exercise
You run where.py and it prints the same path for sys.prefix and sys.base_prefix. Are you inside a virtual environment? What would you expect to see if you were?
Show answer
No. When the two values are equal, the interpreter is the base installation. Inside a virtual environment, sys.prefix points to the environment directory (for example, the .venv folder), while sys.base_prefix still points to the base installation the environment was built from. The site-packages entry in sys.path would also be inside the environment.
Exercise
A package in your virtual environment fails to import because it needs the system library libxml2, which is missing from the operating system. Will deleting and recreating the environment fix this? Why or why not?
Show answer
No. A virtual environment isolates only Python packages. System libraries such as libxml2 come from the operating system and are shared, so recreating the environment does not add them. You need to install the library with the operating system's package manager.