Background

  • I end up writing about using poetry a lot
  • I almost always use direnv in real life too
  • I don’t keep writing mini scripts in my .envrc

Honestly there’s nothing here anyone using the direnv wiki will find surprising, but then it is still neat to link back to.

Setting Up Poetry

This essentially works by simply modifying the global .direnvrc which essentially gets sourced by every local .envrc anyway.

vim $HOME/.direnvrc

So what we put in there is the following snippet derived from other snippets on the wiki, and is actually now there too.

# PUT this here
layout_poetry() {
  if [[ ! -f pyproject.toml ]]; then
    log_error 'No pyproject.toml found.  Use `poetry new` or `poetry init` to create one first.'
    exit 2
  fi

  local VENV=$(dirname $(poetry run which python))
  export VIRTUAL_ENV=$(echo "$VENV" | rev | cut -d'/' -f2- | rev)
  export POETRY_ACTIVE=1
  PATH_add "$VENV"
}

Now we can just make .envrc files with layout_poetry and everything will just work™.