Gangmax Blog

Python Poetry

Similar to “Pipenv“, “Poetry“ is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Installation

1
2
# Before running make sure you have "Python" installed.
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -

After running the script, make sure you have the following content in your “~/.zshrc” file, if you’re using “zsh”:

1
export PATH="$HOME/.poetry/bin:$PATH"

Project setup

1
2
3
4
5
6
# For a new project:
poetry new poetry-demo

# For a pre-existing project:
cd pre-existing-project
poetry init

Running

1
2
3
4
# Start the virtual environeent:
poetry shell
# Run script directly:
poetry run python ./run.py

More details can be also found in this post.

Updating Poetry

From here.

1
poetry self update

Uninstall Poetry

From here.

1
2
3
4
# Download the installation script.
wget https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py
# Execute the script with the "--uninstall" argument.
python get-poetry.py --uninstall

Warning

After upgrading “poetry” and running it I got the following warning message:

1
2
3
4
5
> poetry --version
Configuration file exists at /Users/auser/Library/Preferences/pypoetry, reusing this directory.

Consider moving TOML configuration files to /Users/auser/Library/Application Support/pypoetry, as support for the legacy directory will be removed in an upcoming release.
Poetry (version 1.5.1)

I googled it and this post gave me some hints. Here are the instructions I did.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 1. Confirm which directory is used by your "poetry".
> which poetry
/Users/auser/.local/bin/poetry
> ll /Users/auser/.local/bin/poetry
lrwxr-xr-x 1 auser staff 68B Jun 28 11:41 /Users/auser/.local/bin/poetry -> /Users/auser/Library/Application Support/pypoetry/venv/bin/poetry

# 2. Copy the "config.toml" file from the old directory into the target directory where your "poetry" is.
cp /Users/auser/Library/Preferences/pypoetry/config.toml '/Users/auser/Library/Application Support/pypoetry/'

# 3. Confirm it works.
> poetry --version
Poetry (version 1.5.1)

# 4. Remove the old directory.
rm -rf Users/auser/Library/Preferences/pypoetry

Add wheel dependency in “poetry”

From here and here.

If you have some local “wheel” Python library which you want to use in your “poetry” project, update the “pyproject.toml” file by adding the content like below:

1
2
3
4
[tool.poetry.dependencies]
python = "^3.11"
ndfilter = { file = "lib/myfilter-0.1.1-py3-none-any.whl" }
odyssey = { file = "lib/odyssey-0.1.2-py3-none-any.whl" }

After adding such content, you can run “poetry install” to install the dependencies recursively.

Remove virtual environment

Sometimes if you install some packages by mistaken and want to clear the virtual environment then install it again, you can run the following command(from here) before running “poetry install”:

1
poetry env remove --all

Comments