2023-02-27

Adding Specific Version of Library in Poetry

Adding Specific Version of Library in Poetry

In any given Python project, managing and maintaining dependencies becomes crucial for various reasons: application functionality, compatibility with different environments, and replicability of your project. The Python package manager, Poetry, helps you add, update, and remove dependencies with much ease. In this article, I will focus on adding a specific version of a library in Poetry.

Procedure for Adding a Library

To use a specific library in your Python project managed by Poetry, you need to add that library to your project. Poetry has a dedicated command for this purpose, known as the add command. This command is used in the following format:

bash
$ poetry add <library-name>@<version>

Suppose we want to add the requests library version 2.24.0 to our project. Here's the command:

bash
$ poetry add requests@2.24.0

Specifying a Version

While adding a library to your project, specifying the version number provides you with a granular level of control. Poetry offers flexibility in this aspect:

  • @^2.24.0: This will fetch any version compatible with 2.24.0 according to Semantic Versioning. Typically, this means any version that does not modify the left-most non-zero digit.
  • @~2.24.0: This installs any version 'reasonably close to' 2.24.0. This usually includes versions up to but not including the next minor release.
  • @2.24.*: This will fetch any version that starts with 2.24.
  • @>2.24.0: This will fetch any version that is greater than 2.24.0.
  • @<2.24.0: This will fetch any version that is less than 2.24.0.
  • @>=2.24.0: This will fetch any version that is greater than or equal to 2.24.0.
  • @<=2.24.0: This will fetch any version that is less than or equal to 2.24.0.

If you do not specify a version, Poetry will fetch and add the latest version of the library to your project.

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!