Python and Hatch
darosio edited this page Jul 25, 2023
·
1 revision
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:
mkdir new-project
cd new-project
- Create an
.envrc
file withecho 'layout hatch' > .envrc
direnv allow
- Start to work
hatch run | shell | ...
Workflow for an existing project, existing-project
say:
cd existing-project
- Create an
.envrc
file withecho 'layout hatch' > .envrc
direnv allow
- Start to work
hatch run | shell | ...
It is supposed to be used along with pyenv, e.g. after setting pyenv local 3.11.3
.