Skip to content

Lesson 5 of 9 · 22m

Dependency Specifiers: Ranges, Markers, and Upper Bounds

How to write dependency constraints that protect users without freezing them in the past.

Dependency constraints are risk management. Too loose and a future upstream release breaks your users. Too tight and your library conflicts with everything else in the environment. The craft is choosing constraints that express what you actually support.

Know the operators

requests>=2.31,<3       compatible release range, explicit upper bound
requests~=2.31.0        compatible: >=2.31.0,<2.32.0
requests==2.31.0        exact pin
requests!=2.32.0        exclude a known-bad release
urllib3>=1.26; python_version >= "3.10"   environment marker

Libraries generally need ranges. Applications generally need pins and lockfiles. Confusing those two audiences causes most dependency fights.

Upper bounds are a tradeoff, not a virtue

An upper bound says: "I know versions beyond this are unsupported." That is useful when the upstream project has a history of breaking majors. It is harmful when the bound is speculative, because pip cannot install your package alongside something that legitimately requires the newer release.

A practical policy:

dependencies = [
  "pydantic>=2.5,<3",
  "httpx>=0.26,<1",
]

Test the lower bound in CI. Most projects test only the newest resolver result and discover too late that >=2.5 was aspirational. A minimum-version job exposes missing conditional code and undocumented assumptions.

Markers model real environments

Use environment markers for platform-specific or Python-specific dependencies:

dependencies = [
  "pywin32>=306; sys_platform == 'win32'",
  "importlib-metadata>=7; python_version < '3.12'",
]

Do not use markers to paper over optional features — that's what extras are for. Markers answer "what is true about the environment?" Extras answer "what capability did the user request?"

Direct and transitive dependencies are different

You control direct constraints. You do not control the entire graph. If two packages require incompatible ranges of the same dependency, pip fails rather than picking a winner. This is why libraries should stay as permissive as their verified compatibility allows.

Field notes

  • Never depend on a transitive package directly. If you import it, declare it.
  • pip check catches inconsistent installed requirements after the fact, but resolver failure usually happens earlier.
  • pip install 'pkg[extra]' resolves extras as part of the same environment; they are not separate installs.
  • Read resolver errors from the bottom up; the root conflict is often several lines deep.

Was this lesson helpful?

Sign in to rate