Manage Python Project Environment with pyenv and Poetry Feb 17th 2022 Words: 332

Managing Python projects is sheer painful because Python does not have a universal solution for packaging, version control and dependency management.

The following table shows the common used tools and their features.

Tool Name Python Version Virtual Environment Management Package Management Package Install
virtualenv
pip / pipx
setuptools
pyenv
pipenv
poetry

IMO using pyenv for Python version switch and poetry for dependency and virtual environment management is the best solution.

Pyenv

pyenv lets you easily switch between multiple versions of Python. It’s simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.

Installation

1
curl https://pyenv.run | bash

Enable in the shell

1
2
3
4
5
6
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"

# enable plugin in .zshrc
plugins=(... pyenv)

Install Python runtime

1
pyenv install 3.10.2

Specify Python verison for the project

In project working directory

1
2
3
4
pyenv local 3.10.2

# verify
python --version

The command will create a .python-version file in the directory.

Poetry

Poetry is a Python packaging and dependency management tools that comes with a denpendency resolver.

Installation

1
curl -sSL https://install.python-poetry.org | python3 -

Create a toml file

1
poetry init

Install dependencies (also auto create virtual environment)

Activate the virtual environment

1
poetry shell

Poetrify

Poetrify is a tool that convert the Pipfile or requirements.txt to pyproject.toml used by poetry.

Installation

1
pip install poetrify

Convert Pipfile / requirements.txt

1
poetrify generate

Dockerfile

The following is an example dockerfile for deploying the Python project with an pyproject.toml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
FROM python:3.10.2-alpine

WORKDIR /app

# copy poetry.lock to save time resolving dependencies
COPY pyproject.toml poetry.lock .

RUN apk update && \
apk add musl-dev libffi-dev openssl-dev libev-dev gcc cargo && \
pip install poetry && \
poetry config virtualenvs.create false && \
poetry install

COPY ./app/ .

EXPOSE 8080

CMD [ "python", "./main.py" ]

References