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.

Similar Reads

Poetry

Poetry is a tool for managing dependencies and packaging in Python projects. It helps us easily add, remove, and update libraries as per our project needs. Poetry also ensures that all the required libraries are compatible with each other and our project also makes our Python development simpler and more reliable....

Specifying Dependencies

In Poetry, dependencies are specified in the pyproject.toml file, a standard configuration file for Python projects. Dependencies can be listed under the [tool.poetry.dependencies] section for main dependencies and [tool.poetry.dev-dependencies] for development dependencies....

Version Specifications

Exact Version...

Additional Dependency Constraints

Extras...

Development Dependencies

Development dependencies are specified in a separate section to differentiate them from main dependencies:...

Managing Dependencies with Poetry

To install dependencies specified in the pyproject.toml file, use:...

Updating Dependencies

Keeping dependencies up to date is crucial for maintaining security and functionality. Poetry provides commands to update dependencies easily....

Conclusion

Poetry is a powerful tool for managing dependencies in Python projects. It simplifies adding, removing, and updating libraries. By understanding how to specify versions and set constraints, projects can remain stable and compatible with necessary libraries. Whether the project is simple or complex, Poetry helps manage dependencies effectively and avoid common issues....