Home Artificial Intelligence Forget PIP, Conda, and requirements.txt! Use Poetry As a substitute And Thank Me Later

Forget PIP, Conda, and requirements.txt! Use Poetry As a substitute And Thank Me Later

0
Forget PIP, Conda, and requirements.txt! Use Poetry As a substitute And Thank Me Later

2. Dependency specification

To put in dependencies on your project, you’ll now not need to use PIP or Conda, at the least directly. As a substitute, you’ll start using poetry add library_name commands.

Here is an example:

$ poetry add scikit-learn@latest

Adding the @latest flag installs probably the most recent version of Sklearn from PyPI. It is usually possible so as to add multiple dependencies with none flags (constraints):

$ poetry add requests pandas numpy plotly seaborn

The great thing about add is that if the required packages haven’t any version constraints, it is going to find the versions of all packages that resolve, i.e., not throw any errors when installed together. It would also check against the dependencies already laid out in the pyproject.toml.

$ cat pyproject.toml
[tool.poetry]
...

[tool.poetry.dependencies]
python = "^3.9"
numpy = "^1.25.0"
scikit-learn = "^1.2.2"
requests = "^2.31.0"
pandas = "^2.0.2"
plotly = "^5.15.0"
seaborn = "^0.12.2"

Let’s try downgrading numpy to v1.24 and see what happens:

$ poetry add numpy==1.24

...
Because seaborn (0.12.2) will depend on numpy (>=1.17,<1.24.0 || >1.24.0) ...
version solving failed.

Poetry won’t let it occur since the downgraded version would conflict with Seaborn. If this was PIP or conda, they’d gladly install Numpy 1.24 and would grin back at us because the nightmare starts.

As well as to straightforward installations, Poetry provides a flexible syntax for outlining version constraints. This syntax means that you can specify exact versions, set boundaries for version ranges (greater than, lower than, or in between), and pin down major, minor, or patch versions. The next tables, taken from the Poetry documentation (MIT License), function examples.

Caret requirements:

image.png

Tilde requirements:

image.png

Wildcard requirements:

image.png

For much more advanced constraint specifications, visit this page of the Poetry docs.

LEAVE A REPLY

Please enter your comment!
Please enter your name here