← Back

Poetry

Pythonpoetry
Published:


Poetry

Is a tool for creating, updating, and sharing Python projects. It handles dependency resolution, packaging, and publishing. It also creates a virtual environment for each project.

Uses the pip package manager to install packages and has an advanced dependency resolver that checks for compatibility issues. pipenv is another alternative to this feature.

You can combine pyenv with poetry to manage the Python versions and the virtual environments.

Creating a new project

This will create a new project in the current directory. It will ask you for the project name, version, description, author, and license. Which will be stored in the pyproject.toml file.

poetry init

It uses the Python version that is currently active in the terminal. If you want to use a different Python version, you can specify it using the --python flag. (That version needs to be installed on your system)

poetry.lock file will be created to lock the dependencies to a specific version. This is useful when sharing the project with others.

Installing dependencies

Package installed will be added to the pyproject.toml file.

poetry add <package_name>

Installing from pyproject.toml file

Will install, upgrade, and/or downgrade the dependencies to the specified versions in the poetry.lock file.

poetry install

Upgrade dependencies

Will update the dependencies to the latest version and update the poetry.lock file.

poetry update

Running Environment commands

Will run the specified command in the virtual environment.

If you are not using an interactive shell, you can use the environment by /usr/local/bin/poetry run python script.py.

# Run a single command
poetry run <command>

# Run an interactive shell
poetry shell