26 Dec 2019
Poetry is a tool for managing Python dependencies as well as virtual environments.
Poetry can be installed by running:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
Installation will add Poetry on the home directory and make it automatically available on bash:
Poetry is using the standardized file format pyproject.toml.
pyproject.toml file can be created by simply running
poetry init and following the on-screen instructions.
pyproject.toml file in place, let's try to install the popular library
poetry add requests
Poetry will automagically:
requestson the virtualenv
poetry.lockthat holds the dependency tree.
Note: make sure both
poetry.lock are under version control.
Dev dependencies can be installed by using the
--dev flag, for example:
poetry add nose --dev
Poetry can uninstall a dependency by running:
poetry remove requests
Any command can be run on the created virtualenv by using
poetry run, for example:
poetry run python --version
Alternatively a shell can be spawned by running
poetry shell to avoid prefixing every command.
Let's suppose that a project has to be run specifically on Python version 3.7.5. Poetry can make use of pyenv environments or even python executables that are available on the path.
pyenv to get Python 3.7.5:
pyenv install 3.7.5 pyenv local 3.7.5
Update Python version at
[tool.poetry.dependencies] python = "^3.7.5"
Re-install dependencies for Python 3.7.5:
List all existing virtual environments for a project:
poetry env list
Switch a virtual environment:
poetry env use <env-name>
Remove a virtual environment:
poetry env remove <env-name>
publish packages using the respective commands, something that is missing from Pipenv.
Poetry lock file can be exported to other formats like
requirements.txt by using the
Poetry can display package info by using the
Deploying dependencies on Docker containers should not require installing them under a virtualenv.
Poetry is providing a handy command for disabling the default behaviour:
RUN poetry config virtualenvs.create false \ && poetry install --no-dev --no-interaction --no-ansi
Poetry is an awesome tool, it builds upon previous Pipenv features while at the same time is simplifying the virtual environments handling. Since version 1.0 is out it can be considered pretty stable and ready for production.