Environments
Navigate a project and describe how its interpreter, dependencies and configuration are selected.
THE IDEA
Which python am I running?
When you type python, the shell does not know where that program is. It walks the directories listed in the PATH variable from left to right and runs the first match. Everything about virtual environments follows from that one rule.
- /usr/local/binno python here
- /usr/binno python here
- /binno python here
$ which python → nothing: “command not found”
What a virtual environment is
A virtual environment is a folder with its own Python interpreter links and its own site-packages directory, where installed packages live. Create one with python3 -m venv .venv. Packages installed into it stay in it, so two projects can need different versions without fighting.
source .venv/bin/activate only changes your current shell: it puts .venv/bin at the front of PATH, sets VIRTUAL_ENV and adds a prefix to your prompt. deactivate undoes it. Activation is a convenience, not a requirement: .venv/bin/python script.py uses the environment directly.
The classic trap: installed, but cannot import
pip installs into the environment that its own interpreter belongs to. If the pip you ran and the python you import with belong to different environments, pip reports success and the import still fails.
import works: the package sits in the interpreter that is importing it.
This is why which python and which pip should point at the same environment, and why python -m pip is safer: it uses the pip of the interpreter you name.
Check which python and which pip point into the same folder, or run python -m pip install ... so the pip belongs to that exact interpreter.
Assume the pip in your terminal belongs to the python your editor, notebook or script uses.
Chapter context
A terminal runs commands in a current working directory. An environment makes the program's interpreter, dependencies and configuration explicit so a successful run can be repeated elsewhere.
TRY IT YOURSELF
You are in ~/project, which imports requests. The right panel shows what the shell would run for each command, live.
- Ask the shell which python3 it would run
- Check whether a plain python exists. What does the shell say?
- Try pip3 install requests outside any environment and read why it refuses
- Create a virtual environment named .venv inside the project
- Activate it and watch the prompt change
- Confirm python now resolves to a file inside .venv
- Install requests into this environment
- Import requests with the environment active: no error means it worked
- Deactivate, then run the same import with python3 and watch it fail
What your shell would run
python→ command not foundpython3→/usr/bin/python3pip→ command not foundpip3→/usr/bin/pip3
PATH search order
- /usr/local/bin
- /usr/bin
- /bin
Exported variables
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
Why can pip install succeed while import still fails?
Chapter reference: math, worked example & code
This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.
Math and notation
A reproducible run can be described as (source revision, interpreter, dependency resolution, input data, configuration). Equal source code alone is insufficient. For pipelines, compare the records emitted at each stage rather than assuming every command preserves row counts.
Worked example
A project processes 12 lines, filters to 5 matching lines, then counts them. The final count is 5, not 12. In a path puzzle, joining /work/project with data and train.csv identifies the input without changing the real filesystem.
Reference code
The following example uses the Python standard library and does not call a remote service.
from pathlib import PurePosixPath
path = PurePosixPath('/work/project') / 'data' / 'train.csv'
assert str(path) == '/work/project/data/train.csv'
lines = ['keep' if i < 5 else 'drop' for i in range(12)]
assert sum(line == 'keep' for line in lines) == 5
Python examples are provided for study and local execution; this page does not execute Python.
Common mistakes
A virtual environment is not a security sandbox. A lockfile does not preserve external input datasets. Ignoring a secret after committing it does not undo exposure.