Version Specifications
Exact Version
To specify an exact version of a package, use the ==
operator:
[tool.poetry.dependencies]
requests = "==2.25.1"
This ensures that exactly version 2.25.1 of requests
is installed.
Version Ranges
Version ranges allow you to specify multiple acceptable versions of a package. Poetry supports several operators for defining these ranges:
Caret (^) Operator: The caret operator allows for changes that do not modify the left-most non-zero digit.
[tool.poetry.dependencies]
requests = "^2.25.0"
This will match any version from 2.25.0 to less than 3.0.0.
Tilde (~) Operator: The tilde operator allows for updates that do not modify the middle digit.
[tool.poetry.dependencies]
requests = "~2.25.0"
This will match any version from 2.25.0 to less than 2.26.0.
Greater Than (>) and Less Than (<) Operators: These operators can define open-ended ranges.
[tool.poetry.dependencies]
requests = ">2.24.0, <3.0.0"
This will match any version greater than 2.24.0 but less than 3.0.0.
Combining Ranges
You can combine multiple constraints using logical AND operators (,
):
[tool.poetry.dependencies]
requests = ">2.24.0, <2.26.0"
This ensures that the requests
version is greater than 2.24.0 and less than 2.26.0.
Pre-releases and Development Releases
To allow pre-release versions, you can add a pre-release constraint:
[tool.poetry.dependencies]
requests = ">=2.25.0a1"
This will include alpha, beta, and other pre-release versions of requests
.
Understanding Versions, Ranges, and Constraints in Python Poetry
Managing dependencies is a fundamental aspect of maintaining a Python project. Poetry, a powerful dependency management tool, simplifies this process by providing a clear and concise way to declare, manage, and resolve dependencies. Here we check how to specify dependencies in Poetry, adding, removing, updating dependencies, and the use of versions, ranges, and constraints.