Python Environment
This procedure creates a project-local Python environment with pyenv, direnv, and pip.
Requirements
- The shell loads the direnv hook from
base/.bashrc. pyenvis available on$PATH.- The project has a writable directory.
Where packages go
layout python stores the virtual environment under the project .direnv directory. pip installs packages there, not globally.
Build the environment
- Move into the project directory:
- Install the Python version with pyenv:
- Write the local Python version:
- Write the
.envrcfile:
layout pyenv 3.14.5 selects the Python interpreter that pyenv installed.
layout python creates and loads the virtual environment for that interpreter.
Keep this order because the virtual environment must use the selected Python version.
The direnv stdlib documents both functions: direnv-stdlib.
- If the project needs shared variables, source an env-source file in
.envrc:
Load project environments explains this reuse pattern.
- Allow direnv to load the file:
- Install packages into the project environment:
The project now has .python-version, .envrc, and a virtual environment under .direnv.
Maintain packages
- Update pip:
- Update one package:
- Delete one package:
- Install packages from
requirements.txt:
- Freeze installed versions to
requirements.txt:
Reset the environment
- Delete the direnv environment directory:
- Reload direnv:
Direnv recreates .direnv from .envrc, and pip starts with an empty package set.