Moving from Pipenv to Poetry

PIpenv has been my go-to tool for managing Python projects for many a year. But it's getting quite long in the tooth, and it's time to switch. Here's my "Pipenv workflow" and how I might accomplish the same with Poetry:

Creating a new project

Both poetry and Pipenv can do this for me. However, I prefer to do it myself so I have full control of the location of the environment, python version used, etc. I use pyenv to manage the available python versions on my computer.

# Pipenv workflow:
PYENV_VERSION=3.10.7 python3 -m venv .venv
PIPENV_VENV_IN_PROJECT=1 pipenv install -d

# Poetry workflow:
PYENV_VERSION=3.10.7 python3 -m venv .venv
poetry init --no-interaction
poetry config virtualenvs.in-project true
poetry install --no-root

Both of these will set up a virtualenv in repo root (remember to add ".venv" to ".gitignore")

Install a dependency

# Pipenv workflow:
pipenv install requests

# Poetry workflow:
poetry add requests=*

Use an internal feed for internal packages

We use AWS CodeArtifact for some private python packages.

# Pipenv way of adding an internal package repo, add this to Pipfile:

[[source]]
name = 'internal'
url = 'https://aws:$CODEARTIFACT_AUTH_TOKEN@my-codeartifact-id.d.codeartifact.eu-west-1.amazonaws.com/pypi/pip/simple/'
verify_ssl = true

# Poetry way of doing the same, add this to pyproject.toml:

[[tool.poetry.source]]
name = "internal"
url = "https://my-codeartifact-id.d.codeartifact.eu-west-1.amazonaws.com/pypi/pip/simple"
secondary = true

Pipenv will replace environments variables (prefixed with an "$") with their value, so as long as I have an environment variable called "CODEARTIFACT_AUTH_TOKEN" when I run "pipenv install" I will be fine.

Poetry is a bit more intricate, it requires me to use environment variables tailored for the source in question. This is both good and bad I guess, but I need these for my example to work:

export POETRY_HTTP_BASIC_INTERNAL_USERNAME=aws  # will always be "aws"
export POETRY_HTTP_BASIC_INTERNAL_PASSWORD=$CODEARTIFACT_AUTH_TOKEN

Notice that the name of the source in pyproject.toml has to match the name of the environment variables.

Install a dependency from an internal feed

Pipenv workflow:
# Add this to Pipfile:
"my-internal-package" = {index="internal"}
# Then run:
pipenv install

Poetry workflow:
# Add this to pyproject.toml:
my-internal-package = {version = "*", source = "internal"}
# Then run
poetry install --no-root