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.

1vim $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.

 1# PUT this here
 2layout_poetry() {
 3  if [[ ! -f pyproject.toml ]]; then
 4    log_error 'No pyproject.toml found.  Use `poetry new` or `poetry init` to create one first.'
 5    exit 2
 6  fi
 7
 8  local VENV=$(dirname $(poetry run which python))
 9  export VIRTUAL_ENV=$(echo "$VENV" | rev | cut -d'/' -f2- | rev)
10  export POETRY_ACTIVE=1
11  PATH_add "$VENV"
12}

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