Sub topic 3
Declared versus resolved dependencies
This chapter assumes you can use pip and a virtual environment, read a requirements.txt, and have trained a model with a framework such as PyTorch or scikit-learn. It separates two lists that people often treat as one: the packages you ask for and the packages you get.
Two lists, one environment
Every Python project has two dependency lists, even if only one is written down.
- Declared dependencies are the packages you name yourself, usually with loose version rules. Example:
scikit-learn>=1.3. - Resolved dependencies are the complete set of packages, with exact versions, that an installer chose to satisfy your declaration. This set includes every package your packages need, and the packages those need, down to the last one.
A declaration is a request. A resolution is the result of a computation. The same request can produce different results on different days or machines, and that is why the distinction matters for reproducibility.
What a declaration says
A declaration is a list of requirement specifiers. Each one has a package name and, optionally, a version constraint.
| Specifier | Meaning | Typical use |
|---|---|---|
| `numpy` | Any version | Prototyping only |
| `numpy>=1.26` | This version or newer | Stating a minimum feature need |
| `numpy>=1.26,<2` | A range with an upper bound | Avoiding a known breaking release |
| `numpy~=1.26.4` | Compatible release: `>=1.26.4,<1.27` | Allowing patch fixes only |
| `numpy==1.26.4` | Exactly this version | Pinning one package |
You usually write declarations in requirements.in, in pyproject.toml under [project] dependencies, or in a hand-written requirements.txt. A good declaration lists only what your code imports directly. It states constraints that you know are true, such as "needs the Trainer API added in 2.1", and it leaves everything else open.
A declaration describes what your code needs. It does not describe which environment you tested in.
What resolution does
When you run pip install scikit-learn>=1.3, the installer's resolver does the following:
- It asks the package index (PyPI by default) which versions of
scikit-learnexist. - It reads the metadata of a candidate version to learn what that version requires, for example
numpy,scipy,joblibandthreadpoolctl. - It repeats this for each of those packages until every requirement is satisfied by one compatible set.
- It picks the newest version that fits all constraints, and downloads and installs it.
The result is a tree. You declared one package, and the installer may place a dozen on disk. The packages you did not name are transitive dependencies. You never chose them, yet they run inside your training job.
The diagram shows the key fact. The resolved set depends on three inputs, and only one of them is your file. The other two are the state of the index and the machine you run on.
Why the same declaration gives different results
Because the resolver always picks the newest compatible version, its answer changes whenever something upstream changes.
- Time. A new release of a transitive dependency appears on the index. Your declaration still permits it, so tomorrow's install picks it up. Nothing in your repository changed.
- Platform. Some requirements apply only on certain systems. A requirement written like
colorama; sys_platform == "win32"is called a marker. A resolution on Linux can include packages that macOS skips, and the reverse. - Python version. Newer Python versions may select different releases, because a package drops support for older Pythons.
- Binary compatibility. The installer prefers prebuilt wheels. If no wheel matches your platform, pip falls back to building from source, which depends on the compilers and libraries on your machine.
- Index configuration. An extra index URL or a corporate mirror can offer different versions than the public one.
Here is the typical failure. A teammate trains a model in March and gets an accuracy of 0.91. In June you clone the repository, run pip install -r requirements.txt, and get 0.89. The code, data and seeds are identical. A transitive dependency moved from one minor version to the next, and a default changed in it. Without a record of what was resolved in March, you cannot even say what changed.
Seeing the resolved set
You can inspect the resolution of your current environment at any time. The shell command shows every installed package with its exact version:
pip freezeA short Python script shows the link between one declared package and what it pulls in:
from importlib.metadata import requires, version
def show(name: str) -> None:
print(f"{name}=={version(name)}")
for spec in requires(name) or []:
if "extra ==" in spec:
continue # optional extras are not installed by default
print(" requires", spec)
show("scikit-learn")Run it in an environment where scikit-learn is installed. The output lists the exact installed version, followed by the constraints that this version places on other packages. Those constraints are themselves ranges, such as numpy>=1.19.5. So the library's own metadata is another declaration, and the resolver combines it with yours to compute the final answer.
The comparison is useful:
| Property | Declared | Resolved |
|---|---|---|
| Who writes it | You | The resolver |
| Contains | Direct dependencies | Direct and transitive dependencies |
| Versions | Ranges or none | Exact |
| Stable over time | Yes, until you edit it | No, unless you record it |
| Good for | Expressing intent | Reproducing a run |
Why `pip freeze` is not a full answer
It is tempting to save the output of pip freeze > requirements.txt and call it done. That gives you exact versions, which is a real improvement. It has three weaknesses.
- It mixes the two lists. You cannot tell which packages you chose and which arrived by accident, so removing a dependency later leaves orphans behind.
- It records only what is installed on this machine. Markers for other platforms are already evaluated away, so the file may not install elsewhere.
- It carries no integrity information, so nothing checks that the files downloaded later are the same files you used.
The next chapters in this tutorial address these points with lock files and hashes. Here, only the principle matters: keep the declared list as the human-edited source, and keep a resolved list as a generated record.
Common errors
- Declaring everything, including transitive packages. The file becomes long, and constraints you copied from an old resolution conflict with new ones.
- Declaring nothing but the framework name.
torchalone accepts any version, so a major release can change behavior without a warning. - Using loose ranges as if they were pins.
pandas>=2.0looks stable. It permits every future 2.x and 3.x release. - Assuming a passing install means an equal install. Two environments that both install successfully can still hold different versions.
- Forgetting the Python version. The interpreter is an input to resolution, and a declaration usually does not state it. Set
requires-pythoninpyproject.tomlwhen you can.
Exercise
Your requirements.in contains only scikit-learn>=1.3. A colleague installs it in March and you install it in June, and your results differ slightly. Name which list differs between the two environments, and give one reason it can differ even though the file did not change.
Show answer
The resolved list differs. The declaration stayed the same, but the resolver picks the newest compatible version at install time. Between March and June, a new release of scikit-learn or of one of its transitive dependencies, such as numpy or scipy, can appear on the index, and the June install picks it up.
Exercise
Why is the output of pip freeze not a good replacement for a hand-written declared list?
Show answer
It mixes direct and transitive packages, so you cannot see which ones you chose. It also records only the current machine's platform and carries no hashes. A declared list should hold only your direct requirements, and a resolved list should be generated from it.