Skip to content
darosio edited this page Jul 25, 2023 · 1 revision

Hatch

Similar to layout_pdm, but uses hatch to build a virtualenv from the pyproject.toml located in the same directory.

layout_hatch() {
    PYPROJECT_TOML="${PYPROJECT_TOML:-pyproject.toml}"
    if [[ ! -f "$PYPROJECT_TOML" ]]; then
        log_status "No pyproject.toml found. Executing \`hatch new --init\` to create a \`$PYPROJECT_TOML\` first."
        hatch new --init
    fi

    VIRTUAL_ENV=$(hatch env find)
    PY_VER=$(pyenv local | head -1)

    if [[ -z $VIRTUAL_ENV || ! -d $VIRTUAL_ENV ]]; then
        log_status "No virtual environment folder yet. Executing \`hatch env create\` to create it."
        HATCH_PYTHON="${PYENV_ROOT}/versions/${PY_VER}/bin/python" hatch env create
    fi

    PATH_add "$VIRTUAL_ENV/bin"
    export HATCH_ACTIVE=1
    export VIRTUAL_ENV
}

Workflow for a new project, new-project say:

  1. mkdir new-project
  2. cd new-project
  3. Create an .envrc file with echo 'layout hatch' > .envrc
  4. direnv allow
  5. Start to work hatch run | shell | ...

Workflow for an existing project, existing-project say:

  1. cd existing-project
  2. Create an .envrc file with echo 'layout hatch' > .envrc
  3. direnv allow
  4. Start to work hatch run | shell | ...

It is supposed to be used along with pyenv, e.g. after setting pyenv local 3.11.3.