Manage Your Python Project Using Poetry
Posted on Mar 02, 2019 in Computer Science
Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.
uv (implemented in Rust) is the new cool kid now and is prefer to poetry.
Tips and Traps
-
Python Poetry is current the best project management tool for Python!
-
Python Poetry supports Python package dependencies on GitHub. For example, if a Python package depends on https://github.com/dclong/dsutil, then you can add it using the following.
poetry add git+https://github.com/dclong/dsutil.gitOr
poetry add git+ssh://git@github.com/dclong/dsutil.gitFor more details, please refer to poetry add and git dependencies .
-
poetry installremoves non needed libraries. A tricky situation is that if you have dependency A which depends on dependency B, and you have specified both A and B inpyproject.toml. Removing dependency B frompyrpoject.tomland then runningpoetry installwon't remove the library B from the virtual environment as B is still needed by A. -
Poetry has lots of issues in Windows currently. It is suggested that you avoid using poetry in Windows.
-
If you encounter the following error message when running
poetry install,[Errno 2] No such file or directory: '/path/to/readme.md'
it means that you have specified
tool.poetry.readmeto bereadme.mdinpyproject.tomlbutreadme.mddoes not exists under root directory of the project.
Install Python Poetry
-
Follow the official tutorial.
-
Using xinstall.
# install xinstall if it hasn't been installed sudo pip3 install -U git+https://github.com/dclong/xinstall@master # install poetry using xinstall xinstall pt -ic
Updating Python Poetry
Updating poetry to the latest stable version is as simple as calling the self:update command.
poetry self:update
If you want to install prerelease versions, you can use the --preview option.
poetry self:update --preview
Usage
Create/Initialize a New Project
-
Create a new Python project using poetry.
poetry new proj -
Initialize an existing Python project using poetry.
poetry init
Install Dependencies
-
Installl all dependencies.
poetry install -
Installl all but dev dependencies.
poetry install --no-dev
Export Dependencies
- Export the lock file to
requirements.txtso that the dependency can be installed usingpip.poetry export -f requirements.txt > requirements.txt
Run Commands in the Virtual Environment
poetry run cmd is a quick to run cmd using the virtual environment managed by poetry.
Another way is to manually setPATHbefore you invokecmd`.
For example,
PATH=.venv/bin:$PATH cmd
-
Run test suits using pytest.
poetry run pytestOr if you want to make it specific to collect test suits from the
testdirectory under the root directory of the project.poetry run pytest test -
Run pytype.
poetry run pytype .
User Tasks
https://github.com/sdispater/poetry/pull/591
https://github.com/sdispater/poetry/issues/241
Configuration
https://python-poetry.org/docs/cli/#config
https://python-poetry.org/docs/configuration/
Pleaser refer to pyproject.toml for examples of configuration for Python Poetry.
Restrict Operating Systems in Python Poetry
How can I specify dependencies on operating system in Python Poetry?
[tool.poetry]
# ...
classifiers = [
"Operating System :: POSIX :: Linux",
]
https://python-poetry.org/docs/pyproject/#classifiers
https://pypi.org/classifiers/
https://github.com/python-poetry/poetry/issues/738
https://github.com/python-poetry/poetry/issues/3356
References
https://github.com/sdispater/poetry/issues/522
https://github.com/sdispater/poetry/issues/655
https://github.com/sdispater/poetry/issues/621
https://codingdose.info/2018/08/02/develop-and-publish-with-poetry/
https://hackersandslackers.com/poetic-python-project-packaging/