Sub topic 1
Why projects need their own environments
Prerequisites
This chapter assumes you can do the following:
- Run commands in a terminal or shell.
- Run a Python script and import a module.
- Run
python3(orpyon Windows) from the command line.
You don't need to know pip or venv yet. Later chapters introduce both.
The working model: one interpreter, one set of packages
When you install Python, you get an interpreter. The interpreter also has a directory named site-packages, where third-party packages are installed. When you run import requests, Python searches its path, finds the copy in site-packages, and loads it.
An interpreter has exactly one site-packages directory, and that directory holds at most one version of each package. This is the root of the problem.
You can see which installation your interpreter uses:
python3 -c "import sys; print(sys.prefix)"
python3 -m pip listThe first command prints the installation directory. The second lists every package installed there. If you install packages without any isolation, all your projects share that one list.
How shared installs cause conflicts
Suppose two projects need different major versions of the same library:
- Project A is an older application that works only with
django<4. - Project B is a new application that needs
django>=4.2.
With a single shared site-packages, the second install replaces the first:
python3 -m pip install "django<4"
python3 -m pip install "django>=4.2"After the second command, Project A imports a Django version it was never tested with, and it may fail. Reinstalling the old version then breaks Project B. You can only keep one project working at a time.
Other problems with a shared install
Version clashes are the most visible problem. These are the other common ones:
- Hidden dependencies. Your script may import a package you installed for another project. It works on your machine and fails on a colleague's, because nothing records that you need the package.
- Transitive conflicts. Two packages you use may each require a different version range of a third package. A shared install gives you no room to experiment with these combinations safely.
- System breakage. Many Linux distributions and macOS tools rely on the system Python. Changing its packages can break those tools. Recent distributions often refuse system-wide
pip installwith an "externally-managed-environment" error to protect against this. - Permissions. Installing into a system location often needs administrator rights, which you should avoid using for routine development.
What an environment changes
A virtual environment is a separate directory with its own site-packages, tied to a Python interpreter. When you run the environment's interpreter, it looks for packages in that directory instead of the shared one. Each project gets its own environment, so each project has its own package set, and an install in one project cannot affect the others.
A virtual environment is not a copy of Python or a container. It is a lightweight directory that points back to a base installation, so creating one is fast and uses little disk space. The next chapters cover how to create and use one.
Common mistakes at this stage
- Assuming "it works" means "it's declared". A package that imports fine may come from a leftover global install. An isolated environment shows which packages you really need.
- Treating one environment as a global toolbox. If you put every project's packages into one environment, you recreate the shared-install problem.
- Installing with sudo. Running
sudo pip installmodifies the system interpreter and is rarely the right fix.
Exercise
Project A needs Django below version 4 and Project B needs Django 4.2 or later. Both use the same interpreter and no virtual environments. Why can't both work at once, and what removes the problem?
Show answer
An interpreter has one site-packages directory that holds only one version of each package, so installing the newer Django replaces the older one. Giving each project its own virtual environment, with its own site-packages, lets each project keep the version it needs.