Version Range Specifiers (>=, <, !=)
Convert requirements.txt with compound version ranges like >=1.0,<2.0 to pyproject.toml. Learn all PEP 440 version specifiers and their semantics.
Detailed Explanation
Version ranges allow you to specify flexible dependency bounds. PEP 440 defines several comparison operators that can be combined with commas to create compound specifiers.
requirements.txt with ranges:
django>=4.2,<5.0
sqlalchemy>=2.0,!=2.0.0
celery>=5.3,<6
redis>=4.5
pydantic>=2.0,<3.0
Converted to pyproject.toml:
[project]
dependencies = [
"django>=4.2,<5.0",
"sqlalchemy>=2.0,!=2.0.0",
"celery>=5.3,<6",
"redis>=4.5",
"pydantic>=2.0,<3.0",
]
PEP 440 version specifiers reference:
| Operator | Meaning | Example |
|---|---|---|
== |
Exact match | django==4.2.8 |
>= |
Minimum version | flask>=3.0 |
<= |
Maximum version | numpy<=1.26 |
!= |
Exclude version | sqlalchemy!=2.0.0 |
~= |
Compatible release | requests~=2.31 |
> |
Strictly greater | click>8.0 |
< |
Strictly less | django<5.0 |
Compound specifiers combine multiple operators with commas. For example, >=4.2,<5.0 means "at least 4.2 but below 5.0." This is the most common pattern for major-version pinning: accepting patches and minor releases while avoiding breaking changes in the next major version.
The != operator is useful for excluding known-buggy releases. In the example above, sqlalchemy>=2.0,!=2.0.0 skips the initial 2.0.0 release (which had known issues) while accepting all subsequent 2.x releases.
Use Case
Converting a library's dependency specification that uses flexible version ranges to ensure compatibility across multiple environments and Python versions.