Preface
I read some articles discussing Poetry recently, and I noticed that pyproject.toml seems to be the future trend (PEP 518, PEP 621), so I find some time study it.
What is Poetry
According to Poetry GitHub
Poetry helps you declare, manage and install dependencies of Python projects, ensuring you have the right stack everywhere.
It supports Python 3.7+
Similar to pip
, provides package management but more powerful functions than pip
- Track/Build/Publish for packages
- DEPENDENCY RESOLVER
- ISOLATION
- INTUITIVE CLI
Past Practices and Defects
I used pipenv as a virtual environment management tool for Python, but it is not good for package dependency resolution.
Supposing you need to remove a package, pipenv uninstall
will only remove the specified package, and the dependency packages will not be removed.
You need to remove it manually, but you can not determine whether it is also depended on by other packages.
If you remove it casually, other packages may not work properly.
As kyo described in this article (zh-tw), if flask and black are used in the same project, removing flask
and then manually removing click will cause black
to not work properly.
Start Poetry journey
Install Poetry
Linux
|
|
Windows powershell
|
|

For Windows, it will be installed in %APPDATA%\Python\Scripts
, and this path will also be set to the environment variable PATH
during installation.

Check the version of poetry
|
|
Upgrade the version of poetry
|
|
Check config of poetry
|
|
The cache-dir
is the the path to the cache directory used by Poetry.
If set virtualenvs.in-project
to true
, Poetry
will create the virtualenv inside the project root.
|
|
However, at present, if in-project
is set to true
, there seems to be a BUG when using the poetry-command to remove the virtual environment and it cannot be removed normally, but we can still remove the virtual environment by manually deleting the folder.
Create new project
Create a new poetry
project
|
|
The folder tree will be
|
|
The pyproject.toml
is the most important file
In addition, if there is a pre-existing project, you can create pyproject.toml
via init
|
|
And then we create virtual environment
|
|
Then active the virtual environment (there must be pyproject.toml
in the folder, otherwise an error will be occurred)
|
|
Some themes of Terminal
supports hints if you are in a virtual environment

You can also use poetry env info
to get current virtual environment information
|
|
Exit virtual environment
|
|
Add packages
Use poetry add
to add packages
|
|

pyproject.toml
will be updated at the same time
|
|
It can be observed from the above that other packages are also installed when requests
is installed, such as certifi
, urllib3
, etc. These are dependencies of requests
and will not be recorded in pyproject.toml
We can use the poetry show
command to observe the package dependencies, which are derived from poetry.lock
|
|

If you want to remove the package, you need to use the poetry remove
command
|
|
The removed part will not be demonstrated here, you can try to install the above mentioned flask
and black
libraries at the same time and then remove one of them to see what happens
Run
We can execute commands in the virtual environment through run
For example, the following command can list the version of Python
being executed
|
|
Let’s create a simple script (main.py
)
|
|
|
|
It can be executed via poetry run python
|
|
We can also execute commands by adding Script
Modify pyproject.toml
, add tool.poetry.scripts
|
|
Then we can execute these commands
|
|
Output requirements.txt
If you need to output the package as requirements.txt
, you can generate it with the following command
|
|
Common commands
poetry new PROJECT_NAME
poetry init
poetry env use python
poetry shell
poetry add LIBRARY_NAME
poetry install
poetry remove LIBRARY_NAME
poetry show --tree
poetry export -f requirements.txt --output requirements.txt --without-hashes
Conclusion
The current version of poetry
is about 1.1.13
, and it seems that it is still being optimized. There may be some potential bugs, such as the previously mentioned problem that env cannot be removed. I hope it will get better and better in the future.
For me, I may substitute poetry
for pipenv
, although I still need to get familiar with it, but from all aspects, poetry
seems to be the best solution and trend at present, of course I have to try it.