Skip to content

Instantly share code, notes, and snippets.

@wadinj
Created August 26, 2024 09:39
Show Gist options
  • Save wadinj/3b315f550bccaaf52131d58e72d24548 to your computer and use it in GitHub Desktop.
Save wadinj/3b315f550bccaaf52131d58e72d24548 to your computer and use it in GitHub Desktop.
Direnv load poetry environment
layout_poetry() {
PYPROJECT_TOML="${PYPROJECT_TOML:-pyproject.toml}"
if [[ ! -f "$PYPROJECT_TOML" ]]; then
log_status "No pyproject.toml found. Executing \`poetry init\` to create a \`$PYPROJECT_TOML\` first."
poetry init
fi
if [[ -d ".venv" ]]; then
VIRTUAL_ENV="$(pwd)/.venv"
else
VIRTUAL_ENV="$(poetry env list --full-path | head -1)"
fi
if [[ -z $VIRTUAL_ENV || ! -d $VIRTUAL_ENV ]]; then
log_status "No virtual environment exists. Executing \`poetry install\` to create one."
poetry install
VIRTUAL_ENV=$(poetry env info --path)
fi
PATH_add "$VIRTUAL_ENV/bin"
export POETRY_ACTIVE=1 # or VENV_ACTIVE=1
export VIRTUAL_ENV
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment