This guide explains how to set up isolated Python environments for each project, using asdf for Python version management, direnv for automatic .venv activation, and 1Password CLI for secure secret handling.
brew install asdf direnv uv 1password/tap/1password-cli
op account add && op signinEnsure your ~/.zshrc has:
if command -v direnv >/dev/null 2>&1; then
eval "$(direnv hook zsh)"
fi
alias oprun='op run --env-file .env --'And your ~/.direnvrc contains:
usepy() {
if [ -r /opt/homebrew/opt/asdf/libexec/asdf.sh ]; then
. /opt/homebrew/opt/asdf/libexec/asdf.sh
elif [ -r "$HOME/.asdf/asdf.sh" ]; then
. "$HOME/.asdf/asdf.sh"
fi
VENV=".venv"; [ ! -d "$VENV" ] && python -m venv "$VENV"
. "$VENV/bin/activate"
}- Create project directory and move into it:
mkdir myproject && cd myproject- Set Python version (change as needed):
asdf local python 3.12.5- Enable direnv and
.venv:
echo 'usepy' > .envrc
direnv allow- Create
.envand reference secrets from 1Password:
GOOGLE_KEY=op://<Vault>/<Item>/google_key.txt- Install dependencies:
uv pip install <package>- Run scripts with secrets loaded at runtime:
oprun uv run python script.py- Isolated: Each project has its own
.venv - Reproducible:
.tool-versionsensures consistent Python version - Secure: Secrets never stored in plain text — use
op://references - Automatic:
direnvactivates.venvwhen youcdinto the folder
- Reinstall prerequisites (see above)
- Restore
~/.direnvrcand~/.zshrcfrom backup cdinto project →direnv allow- Install dependencies:
uv pip install -r requirements.txt