Created
October 28, 2025 22:01
-
-
Save gretel/ae45e13f563dc9d7f69df9589eef8639 to your computer and use it in GitHub Desktop.
python3 layout module for https://direnv.net/ - using uv and allows to use a specific python version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| layout_uv() { | |
| local python_version="${1:-}" | |
| local should_recreate=0 | |
| # Check if we need to recreate venv due to version mismatch | |
| if [[ -d ".venv" && -n "$python_version" ]]; then | |
| local current_version | |
| if [[ -f ".venv/bin/python" ]]; then | |
| current_version=$(.venv/bin/python --version 2>&1 | grep -oE '[0-9]+\.[0-9]+') | |
| elif [[ -f ".venv/Scripts/python.exe" ]]; then | |
| current_version=$(.venv/Scripts/python.exe --version 2>&1 | grep -oE '[0-9]+\.[0-9]+') | |
| fi | |
| if [[ -n "$current_version" && "$current_version" != "$python_version"* ]]; then | |
| log_status "Python version mismatch (current: $current_version, requested: $python_version). Recreating venv." | |
| rm -rf .venv | |
| should_recreate=1 | |
| fi | |
| fi | |
| if [[ ! -d ".venv" || $should_recreate -eq 1 ]]; then | |
| if [[ ! -f "pyproject.toml" ]]; then | |
| log_status "No uv project exists. Executing \`uv init\` to create one." | |
| uv init --no-readme | |
| rm -f main.py | |
| fi | |
| if [[ -n "$python_version" ]]; then | |
| log_status "Creating venv with Python $python_version" | |
| uv venv --python "$python_version" | |
| else | |
| uv venv | |
| fi | |
| # Install pip into the venv | |
| uv pip install --python .venv/bin/python pip | |
| if [[ -f "pyproject.toml" ]]; then | |
| uv sync | |
| fi | |
| fi | |
| VIRTUAL_ENV="$(pwd)/.venv" | |
| # Ensure uv is in PATH first | |
| if command -v uv >/dev/null 2>&1; then | |
| PATH_add "$(dirname "$(command -v uv)")" | |
| fi | |
| if [ -d ".venv/bin" ]; then | |
| PATH_add .venv/bin | |
| elif [ -d ".venv/Scripts" ]; then | |
| PATH_add .venv/Scripts | |
| fi | |
| export UV_ACTIVE=1 | |
| export VIRTUAL_ENV | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example
.envrc