poetry landscape
A quick and easy guide to managing and packaging Python dependencies
Poemsis a wonderful open source tool that can make it easier for you to set up your environment and configure your dependencies.
In this blog I take a small look at the possibilities of poetry and the power that lies within it. I will also walk you through the technical process of setting up the Poetry environment in PyCharm.
Poetry has several advantages over some existing packaging and dependency management tools (seepoetry introductionja poetic apology). Perhaps the main reason for poetry's growing popularity can be found in the following quote from the Introduction to Poetry:
Poems
is a tool for installing dependencies, building and packaging Python packages. You only need one file for this: the new,standardizedpyproject.toml
.In other words, poetry used
pyproject.toml
substituteconfiguration.py
,Requirements.txt
,Konfiguration.cfg
,MANIFEST.in
jPip-Datei
.
There are many different configuration options for using Poetry to manage Python packages and dependencies. The one that I will present in this blog is the configuration that I found the most comfortable and that meets all my needs.
follows the poemDocumentationto install Poetry according to your system environment.
For PyCharm versions >=2021.3 Poetry is already integrated without manual plugin installation.
For PyCharm versions < 2021.3 we install the required plugin for PyCharm. Below is the list of Poetry versions for the different JetBrains IDEs.
Poetry - Plugin IntelliJ IDE | I'm going to buy
This plugin supports Poesie, a package manager for the Python help. See the documentation for more details.
plugins.jetbrains.com
After clicking the link above, a pop-up window will appear in the IDE with instructions on how to install the plugin.
We are ready to create our new Poetry environment! In this example I create a package calledassessmentsused for both training and maintenance of our text classification model that extracts quality of life information in apartment complexes from tenant reviews (Haber e Waks, 2021 See more).
Poetry new reviews
The result of this step is thepyproject.toml
Office hours:
[tool.poetry]
name="comments"
version="0.1.0"
Description = ""
autores = ["hai.rozencwajg <EMAIL>"][tool.poetry.dependencies]
python="^3.7"
[tool.poetry.dev-dependencias]
Pytest = "^5.2"
[Bausystem]
needed="["core-poetry>=1.0.0"]
build-backend = "poetry.core.masonry.bee"
My installed Python version is3.7.9
, and the Python version at the[tool.poetry.dependencies]
the section is^3.7
, that is, Python versions equal to or greater than3.7
it is smaller than4
Execution.
To read more about symbols in poetry, like the cursor (^
) and additional version restrictions, seePoetry dependency specificationManual.
Next, let's create a Poetry environment for this project by running the following command:
Poem installation
This will create a local virtual environment that will be used to run the project based on the settings listed inpyproject.toml
office hours.
The locale was created in the default Poetry locale path~/Biblioteca/Caches/pypoetry/virtualenvs
.
After clicking onAdd interpreterIn PyCharm, the existing interpreter was automatically found and the Poetry virtual environment was automatically activated for this project.
similarly famousInstall pipe
Poetry command also supports easy installation of Python packages with PoetryaddDomain.
poetry adds google-cloud-storage
After running the above, a new line was added topyproject.toml
File documenting the version for theGoogle cloud storage
Package.
[tool.poetry]
name="comments"
version="0.1.0"
Description = ""
autores = ["hai.rozencwajg <EMAIL>"][tool.poetry.dependencies]
python="^3.7"
Google cloud storage = "^2.4.0"
[tool.poetry.dev-dependencias]
Pytest = "^5.2"[Bausystem]
needed="["core-poetry>=1.0.0"]
build-backend = "poetry.core.masonry.bee"
aside from thatpoem.lock
file was created. This file keeps track of the installed versions of packages and their dependencies as indicated in the following log:
Using version ^2.4.0 for Google cloud storageUpdate dependencies
Dependencies are resolved... (9.7s)File Write LockBundle Operations: 18 installs, 0 upgrades, 0 removals• Installation of pyasn1 (0.4.8)
• Installation von Caching-Tools (5.2.0)
• Installation of certificates (2022.6.15)
• Installation des Character Set Normalizer (2.1.0)
• idna-Installation (3.3)
• Install protobuf (4.21.2)
• Installation of pyasn1 modules (0.2.8)
• Installation of rsa (4.8)
• Installation of six (1.16.0)
• Installation of urllib3 (1.26.10)
• Installation von Google-Auth (2.9.0)
• Installation von googleapis-common-protos (1.56.3)
• Installation Prompts (2.28.1)
• Installation von google-api-core (2.8.2)
• Installation von google-crc32c (1.3.0)
• Installation von google-cloud-core (2.3.1)
• Installation of resumable media from Google (2.3.3)
• Google Cloud Storage-Installation (2.4.0)
Next I wanted to add thegoogle-cloud-bigquery
Pack for Poetry, so I ran PoetryaddDomain:
add poetry google-cloud-bigquery
But this time I got an error that demonstrates the power of poetry:
Using version ^3.2.0 for google-cloud-bigqueryUpdate dependencies
Dependencies are resolved... (0.2s)SolverProblemErrorThe current project's Python requirement (>=3.7,<4.0) is incompatible with some of the Python requirements of the required packages:
- google-cloud-bigquery requires Python >=3.6, <3.11, so you won't be satisfied with Python >=3.11,<4.0Since for google-cloud-bigquery (3.2.0) Python >=3.6, <3.11
de ~/.poetry/lib/poetry/puzzle/solver.py:241 de _solve
and no version of google-cloud-bigquery matches >3.2.0,<4.0.0, google-cloud-bigquery is forbidden.
As revisions depend on google-cloud-bigquery (^3.2.0), version resolution therefore failed.
237│packets = result.packets
238│ except OverrideNeeded as e:
239│ devuelve self.solve_in_compatibility_mode(z.B. overrides, use_latest=use_latest)
240│ except SolveFailure as and:
→ Raise 241│ SolverProblemError(s).
242│
243│ results = dict(
244│ deep_first_search(
245│ PackageNode(self._package, pacotes), Added_package_nodes• Check the Python requirement of your dependencies: The Python requirement can be specified using the `python` or `markers` propertiesFor google-cloud-bigquery, a possible solution would be to set the 'python' property to '>=3.7,<3.11'.
https://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies,
https://python-poetry.org/docs/dependency-specification/#using-environment-markers
It turns out that there is a compatibility issue between the latest version ofgoogle-nube-bigquery==3.2.0
and the possible Python versions listed inpyproject.toml
office hours.
Luckily for us, Poetry also offers us a possible solution:
For google-cloud-bigquery, a possible solution would be to set the 'python' property to '>=3.7,<3.11'.
As we can see above, Poetry asks us to be more strict with the Python version and limit ourselves to>=3,7, <3,11
instead of the current setting^3.7
, so I changed the python version topyproject.toml
file as suggested.
[tool.poetry]
name="comments"
version="0.1.0"
Description = ""
autores = ["hai.rozencwajg <EMAIL>"][tool.poetry.dependencies]
python = ">=3.7,<3.11"
Google cloud storage = "^2.4.0"
[tool.poetry.dev-dependencias]
Pytest = "^5.2"
[Bausystem]
needed="["core-poetry>=1.0.0"]
build-backend = "poetry.core.masonry.bee"
Now, after re-executing the poetryaddcommand for thegoogle-cloud-bigquery
Package successfully completed installation.
Using version ^3.2.0 for google-cloud-bigqueryUpdate dependencies
Dependencies are resolved... (91.8 s)File Write LockPackage operations: 8 installs, 1 update, 0 removes• Protobuf-Update (4.21.2 -> 3.20.1)
• Install grpcio (1.47.0)
• Install grpcio-status (1.47.0)
• Install numpy (1.21.6)
• Installation of proto-plus (1.20.6)
• Installation von google-cloud-bigquery-storage (2.14.0)
• Installation of Pyarrow (8.0.0)
• Installation of python-dateutil (2.8.2)
• Installation von google-cloud-bigquery (3.2.0)
We can also see that theProtobuf
The version has been downgraded (automatically) to support all our version compatibility limitations.
The newpyproject.toml
File now looks like this:
[tool.poetry]
name="comments"
version="0.1.0"
Description = ""
autores = ["hai.rozencwajg <EMAIL>"][tool.poetry.dependencies]
python = ">=3.7,<3.11"
Google cloud storage = "^2.4.0"
google-nube-bigquery = "^3.2.0"
[tool.poetry.dev-dependencias]
Pytest = "^5.2"
[Bausystem]
needed="["core-poetry>=1.0.0"]
build-backend = "poetry.core.masonry.bee"
Install a specific package version
similar tolump
, Poetry supports installing certain package versions:
Poetry add pandas ~= 1.3
Installing packages for development environments only
It's fun to say goodbye to so manyRequirements.txt
Poetry files support management of production and development environments in a single file. install downPanda-Profile
Only use in the development environment for data exploration.
poetry add pandas profiles --dev
ÖPanda-Profile
The package is listed below[tool.poetry.dev-dependencias]
section instead of below[tool.poetry.dependencies]
.
uninstall packages
Uninstalling is as easy as adding packages:
delete poetry google-cloud-bigquery
Open a new shell and start it with a Poetry environment
poetry shell
Poetry is a great, feature-packed tool that allows you, as a developer, to manage your dependencies in a clean, easy, and transparent way. Using Poetry makes it easier to configure your environment and helps resolve compatibility issues.
I would definitely continue to use poetry for my future projects and I hope you feel the same way.
Hello Rozencwajis a leading data scientist inHorizon OFF, oneJLLcompany and part ofJLL Technologies. We use broad and diverse information along with cutting-edge technology to provide real estate professionals with insights that create unique business value.