Avoid relative path import hell in python

Exploring poetry for dependency management in python

In general pip & venv is a good combination of tool when you don’t have to manage multiple dependencies for your project. But imaging that in a project you need to management multiple dependency files to deploy code into multiple envs. It is possible to do this with pip, but in that case you need to manage multiple requirements files. To solve this project I have checked a few alternative like pyenv, pipx, pipenv, poetry etc. According to my experience, poetry is the simplest and most efficient one. I was checking some of the useful tutorials about this and here I am just taking a note of some of the useful points regarding this tool.

Some useful poetry commands

# Download poetry in Ubuntu
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
source $HOME/.poetry/env # Add to PATH
poetry --version # Check version of poetry
poetry self update # Update version
poetry new project1 # Create a new project
cd project1
tree . 
poetry run pytest # Run pytest for the project
poetry add pandas # Add a package as dependency of a project
poetry remove pandas # Delete a project from the file
poetry add --dev pytest # Add a package as dev dependency in a poetry project
poetry add -D coverage[toml] pytest-cov # --dev & -D same
poetry install # Install all the dependencies for a project
poetry build # Build a python library using poetry
poetry publish # Publish library to PyPI
poetry export - requirements.txt --output requirements.txt # Generate requirements.txt
poetry use python3.8 # Use specific version of python in the project

Some important information

Important files

  • pyproject.toml is the single file for all project related metadata.
  • poetry.lock file is the granular metadata.
  • .pypirc will not work with poetry.
  • config.toml & auth.toml is used for setting up the artifact repository.
  • export POETRY_PYPI_TOKEN_PYPI, export POETRY_HTTP_BAISC_PYPI_USERNAME and export POETRY_HTTP_BAISC_PYPI_PASSWORD can be used for this.

Publishing library as artifact to artifact store

# config.toml : ~/.config/pypoetry/config.toml
[repositories]
pypi = {url = "https://upload.pypi.org/legacy/"}
testpypi = {url = "https://test.pypi.org/legacy/"}
# auth.toml: ~/.config/pypoetry/auth.toml
[http-basic]
pypi = {username = "myuser", password = "topsecret"}
testpypi = {username = "myuser", password = "topsecret"}

Check GitHub issue related to this here.

Reference:

Aritra Biswas
Aritra Biswas
Senior ML Engineer

My research interests include computational statistics, causal inference, simulation and mathematical optimization.

Related