Unit 11 · lesson
Packages, `pip`, and Virtual Environments
Packages, pip, and Virtual Environments
Core path: 25 minutes
How can a project use Python code that does not ship with Python without turning the whole computer into one giant dependency pile?
Standard library versus third-party package
Some modules come with Python:
import json
import pathlib
import random
Other packages must be installed separately. requests is one example.
That gives us three different actions that beginners often mix together:
CREATE/SELECT ENVIRONMENT
↓
INSTALL PACKAGE
↓
IMPORT PACKAGE IN CODE
import requests does not install anything. pip install requests does not automatically add an import statement to your program.
Isolate the project with venv
Create a virtual environment in the project root:
python -m venv .venv
A virtual environment gives the project its own Python package location. That means Project A can depend on one set of packages without silently changing Project B.
Depending on the workspace, your editor may activate .venv automatically. If it does not, the common commands are:
Windows PowerShell: .venv\Scripts\Activate.ps1
macOS/Linux: source .venv/bin/activate
Verify which Python you are using with the environment tools provided by your workspace before installing anything.
Install with the selected interpreter
Use:
python -m pip install requests
Then Python code can import it:
import requests
print(requests.__version__)
If you install into one interpreter and run the program with another, you can still get ModuleNotFoundError. That failure is not proof that pip is broken. It is evidence that your install environment and execution environment may not match.
Track dependencies
A simple requirements.txt records what another environment needs:
requests
Install from it with:
python -m pip install -r requirements.txt
The file communicates dependencies. It does not contain the packages themselves.
Guided example: diagnose an import that "should work"
You run:
python -m pip install requests
Then another terminal reports:
ModuleNotFoundError: No module named 'requests'
Do not reinstall five times. Check the environment chain:
Which Python ran pip?
↓
Which Python runs the program?
↓
Are both inside the same .venv?
The useful fix is to align the interpreter and environment, not to guess.
Dependency cost
Every third-party dependency adds maintenance and supply-chain surface. That does not mean "never use packages." It means the dependency should solve a problem worth depending on.
Vocabulary lab
Flip the idea, not just the card
Explain the term before you reveal the back. Then compare your explanation with the definition, example, and warning.
Read all terms without animation
- Module
- A Python file or importable unit that provides code to another module. Example: json is a standard-library module. Do not confuse it with: A complete isolated Python environment.
- Package
- An installable distribution of Python code and metadata. Example: requests can be installed from the Python package ecosystem. Do not confuse it with: An import statement inside one source file.
- Dependency
- External code or software that a project requires in order to work. Example: A project that imports requests depends on that package. Do not confuse it with: Code written directly inside the project.
- Virtual Environment
- An isolated Python environment used to keep a project's packages separate from other projects. Example: A .venv directory created with python -m venv .venv. Do not confuse it with: One shared global package location for every project.
- pip
- Python's package installation tool used with a selected interpreter. Example: python -m pip install requests. Do not confuse it with: import requests, which only asks Python to load an installed package.