Background
Currently maintaining these two Python wheels:
Previously, I had been using setup.py for packaging, but when updating NJUlogin in November, I noticed a prompt like this:
Therefore, I decided to use the new pyproject.toml approach for packaging. At that time, I temporarily learned Poetry and adapted NJUlogin. Today, while updating mijia-api, I wanted to adapt it as well, but I found that I had forgotten how to use it. So I'll write a blog post to record it. It seems I'll be using Poetry more for dependency management from now on.
Poetry
Poetry's official website describes itself as: Python packaging and dependency management made easy. It feels a bit like npm (if you have some knowledge of Node.js), also managing dependency files via the command line.
But 'poetry' is a bit too long, so from now on let's call it pop.
Installation
Just check the documentation: https://python-poetry.org/docs/#installation
I used pipx for installation here.
Initializing an Existing Project
This produces the following pyproject.toml file
However, because my package contains uppercase letters, I need to change the name under [tool.poetry] and also manually add a packages entry, and additionally bring over some other configurations from setup.py:
Creating a Poetry Virtual Environment and Installing Dependencies
By default, a virtual environment is created in ~/.cache/pypoetry. You can modify this using pop config, see the documentation here. I personally prefer to place it in the project directory:
After that, you can install dependencies one by one from requirements.txt. The following command will write the dependencies into the pyproject.toml file and generate poetry.lock:
A major advantage of Poetry is that it can display dependencies in a tree format:
At this point, you can delete the original setup.py.
Packaging and Publishing
The version can be dynamically specified using poetry-dynamic-versioning, so you don't need to modify pyproject.toml each time to set the version number.
Now you can build with a single command:
Unlike twine which uses ~/.pypirc to store the PyPI token, Poetry requires additional configuration:
Then you can publish!