Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Poetry Setup for Python Projects

Introduction to Poetry

Poetry is a robust tool for Python application package management that aims to improve and simplify the entire packaging process in Python. It allows developers to declare and manage libraries their Python project depends on. But it’s not just a package manager - it’s also a dependency resolver and a build system.

Poetry solves many issues that can be found in other packaging tools by standardizing how dependencies are managed and packages are built. This ultimately leads to more reliable and maintainable Python projects.

Here are some of the reasons why using Poetry is important:

  • Dependency management: Poetry accurately resolves and installs project dependencies. It ensures that your project will always have the correct dependencies and versions, which helps to avoid “dependency hell”.

  • Semantic versioning: Poetry adheres to the principles of semantic versioning. It can intelligently and automatically manage your project version.

  • Reproducibility: Poetry locks your project’s dependencies in a poetry.lock file, ensuring that your project is always in a known state. This makes it easier for other developers to reproduce your environment and reduces “it works on my machine” type issues.

  • Isolated environments: With Poetry, there’s no need for explicit virtualenv management. Poetry can automatically create a virtual environment for your project and manage it for you.

  • Easy publishing: Poetry makes it easy to package and publish your Python projects to PyPI. With a single command, you can build and publish your project.

By providing a consistent, easy-to-use, and robust solution for package management, Poetry is a valuable tool for any Python developer looking to simplify their project setup and ensure reliable dependency resolution.

Install Miniconda and create a new environment with Python

Open a terminal and run the following command to download the latest Miniconda installer for Linux and install it with Python 3.10:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/miniconda3
source $HOME/miniconda3/etc/profile.d/conda.sh
conda update --all -y
conda create --name my_project python=3.10

This will download and install the latest version of Miniconda with Python 3.10, and create a new Conda environment named my_project with Python 3.10.

Install Poetry

Run the following command to install Poetry:

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -

This will download and install the latest version of Poetry.

Create a new Poetry project

Navigate to the directory where you want to create your project. Run the following command to create a new Poetry project:

poetry new my_project

Replace my_project with the name of your project.

Start using Poetry

You can now use Poetry to manage your project dependencies and build your project.

  • Open the pyproject.toml file in your project directory.
  • Add your project dependencies under the [tool.poetry.dependencies] section.
  • Save the file.

Run the following command to install your project dependencies:

poetry install

This will install the dependencies listed in your pyproject.toml file.

That’s it! You now have a Conda environment with Python 3.10 set up, Poetry installed, and you can use Poetry to manage your project dependencies and build your project.

Gotchas

Keyring not working correctly on Ubuntu 20.04

Issue ID: #6354

Summary:

User eduDorus reported that Poetry was not working correctly on Ubuntu 20.04 LTS with Python 3.10.6 and Poetry version 1.2.0. The issue was with the keyring not authenticating unless the keyring backend was set to null. The user received a “KeyringLocked” error message.

Solution:

A solution was provided which involved setting the PYTHON_KEYRING_BACKEND environment variable to “keyring.backends.null.Keyring”.

export PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring