Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
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.git
Or
poetry add git+ssh://git@github.com/dclong/dsutil.git
For more details, please refer to poetry add and git dependencies .
-
poetry install
removes 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.toml
and then runningpoetry install
won'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.readme
to bereadme.md
inpyproject.toml
butreadme.md
does 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.txt
so that the dependency can be installed usingpip
.poetry export -f requirements.txt > requirements.txt
Run Commands in the Virtual Environment
peotry run cmd
is a quick to run cmd
using the virtual environment managed by poetry.
Another way is to manually set
PATHbefore you invoke
cmd`.
For example,
PATH=.venv/bin:$PATH cmd
-
Run test suits using pytest.
poetry run pytest
Or if you want to make it specific to collect test suits from the
test
directory 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/