A Homebrew package that adds layout_uv functionality to direnv, enabling seamless Python environment management using uv.
layout_uv is a direnv layout function that works similar to layout python, but uses the modern and fast uv tool for Python environment management. It automatically creates and activates a Python virtual environment when you enter a directory.
To install uv:
curl -LsSf https://astral.sh/uv/install.sh | shbrew install easytocloud/tap/layout_uv
install-layout-uvThe first command installs the package, and the second command installs the direnv function to ~/.config/direnv/lib/layout_uv.sh.
In your project directory, create or edit .envrc:
# Use default Python version (python3.12)
layout uv
# Or specify a Python version
layout uv python3.11Then allow direnv to load the configuration:
direnv allowWhen you enter the directory, direnv will:
- Create a
.venvdirectory if it doesn't exist (using uv) - Automatically sync dependencies from
pyproject.tomlorrequirements.txt - Activate the virtual environment
- Set appropriate environment variables
The layout_uv function:
- Checks if uv is installed
- Creates a virtual environment using
uv venvwith the specified Python version - Automatically syncs dependencies:
- For
pyproject.tomlprojects: runsuv sync --frozen(oruv syncif lockfile needs updating) - For
requirements.txtprojects: runsuv pip install -r requirements.txt - Uses
watch_fileto only re-sync when dependency files change
- For
- Activates the environment by setting
VIRTUAL_ENVand updatingPATH - Sets
UV_PROJECT_ENVIRONMENTfor uv integration
$ cd myproject
$ echo "layout uv python3.12" > .envrc
$ direnv allow
direnv: loading ~/myproject/.envrc
Creating virtual environment with uv using python3.12
uv environment activated: /Users/username/myproject/.venv
$ which python
/Users/username/myproject/.venv/bin/python
$ python --version
Python 3.12.0- Faster: uv is significantly faster than pip and traditional virtualenv tools (10-100x for large projects)
- Modern: Built with Rust for performance and reliability
- Automatic dependency management: Dependencies stay in sync automatically when switching branches or pulling changes
- Smart syncing: Only re-syncs when dependency files change (using direnv's
watch_file) - Lockfile support: Works with both modern
pyproject.toml+uv.lockand legacyrequirements.txt - Compatible: Works seamlessly with existing Python tooling
- Simple: Same familiar direnv workflow with zero manual dependency management
For new projects, use pyproject.toml for dependency management:
# Initialize a new uv project
uv init
# Add dependencies
uv add requests pandas
# Dependencies will auto-sync when you cd into the directoryThe layout_uv function automatically:
- Runs
uv sync --frozenfor fast installs using the lockfile - Falls back to
uv syncif the lockfile needs updating - Only re-syncs when
pyproject.tomloruv.lockchanges
For existing projects with requirements.txt:
# Dependencies will auto-install when you cd into the directory
# The function runs: uv pip install -r requirements.txt# Convert requirements.txt to pyproject.toml
uv init
uv add -r requirements.txtTo uninstall the Homebrew package:
brew uninstall layout_uvNote: The direnv function file (~/.config/direnv/lib/layout_uv.sh) will remain after uninstallation. To remove it manually:
rm ~/.config/direnv/lib/layout_uv.shSee LICENSE file for details.
Contributions are welcome! Please feel free to submit issues or pull requests.