Skip to content

Instantly share code, notes, and snippets.

@BradenM
Created October 28, 2025 20:58
Show Gist options
  • Select an option

  • Save BradenM/3e450072242899e1a5d59a1e9332b62c to your computer and use it in GitHub Desktop.

Select an option

Save BradenM/3e450072242899e1a5d59a1e9332b62c to your computer and use it in GitHub Desktop.
Direnv python_uv layout helper
#!/usr/bin/env bash
layout_python_uv() {
local python=${1:-python}
[[ $# -gt 0 ]] && shift
unset PYTHONHOME
local python_path=$(uv python find $python)
local python_version=$($python_path -V | cut -d' ' -f 2 | cut -d . -f 1-2)
if [[ -z $python_version ]]; then
log_error "Could not find python's version"
return 1
fi
if [[ -n "${VIRTUAL_ENV:-}" ]]; then
local REPLY
realpath.absolute "$VIRTUAL_ENV"
VIRTUAL_ENV=$REPLY
else
VIRTUAL_ENV=$(direnv_layout_dir)/python-$python_version
fi
export UV_PROJECT_ENVIRONMENT=$VIRTUAL_ENV
if [[ ! -d $VIRTUAL_ENV ]]; then
uv venv -p $python "$@" "$VIRTUAL_ENV"
fi
export VIRTUAL_ENV
PATH_add "$VIRTUAL_ENV/bin"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment