Python Poetry Cheat Sheet
Table of Contents
Poetry is an amazing tool for dependancy management in Python, it solves a lot of problems of pip. Here in this Poetry Cheat Sheet we have useful commands that we may need frequently.
Initializing
New Project
poetry new poetry-demoIn Pre-existing Directory
cd pre-existing-project
poetry initEnvironments
Displaying the environment information
poetry env infoUsing your own virtual environment
Keep virtualenv in project root
This will also let VS Code discover your porject’s virtual env.
poetry config virtualenvs.in-project true --local
External virtual environment management
Poetry will detect and respect an existing virtual environment that has been externally activated. This is a powerful mechanism that is intended to be an alternative to Poetry’s built-in, simplified environment management.
You can use pyenv to create external virtual environment.
Switching between environments
poetry env use /full/path/to/pythonpoetry env use 3.7poetry env use systemListing the environments associated with the project
poetry env listDeleting the environments
poetry env remove /full/path/to/python
poetry env remove python3.7
poetry env remove 3.7
poetry env remove test-O3eWbxRl-py3.7
Use the --all option to delete all virtual environments at once.
poetry env remove --allDependencies
Add new dependency
poetry add <package name>Removing dependency
poetry remove <package name>Installing dependencies
poetry installUpdating dependencies to their latest versions
This will fetch the latest matching versions (according to your pyproject.toml file) and update the lock file with the new versions. (This is equivalent to deleting the poetry.lock file and running install again.)
poetry updateAdding a dependency to a group
poetry add pytest --group testpoetry add isort --group devInstalling group dependencies
without
poetry install --without test, docs
with
poetry install --with docs
only
poetry install --only mainRemoving dependencies from a group
poetry remove mkdocs --group docsSynchronizing dependencies
Poetry supports what’s called dependency synchronization. Dependency synchronization ensures that the locked dependencies in the poetry.lock file are the only ones present in the environment, removing anything that’s not necessary.
poetry install --syncpoetry install --without dev --sync
poetry install --with docs --sync
poetry install --only devExport
poetry export -f requirements.txt --output requirements.txt
-
--format (-f): The format to export to (default:requirements.txt). Currently, onlyconstraints.txtandrequirements.txtare supported. -
--output (-o): The name of the output file. If omitted, print to standard output. -
--extras (-E): Extra sets of dependencies to include. -
--without: The dependency groups to ignore. -
--with: The optional dependency groups to include. -
--only: The only dependency groups to include. -
--without-hashes: Exclude hashes from the exported file. -
--without-urls: Exclude source repository urls from the exported file. -
--with-credentials: Include credentials for extra indices.
Packaging
Build
poetry buildPublish
poetry publishConfiguration
You can find all the configuration options here.