My personal website and Gemini capsule
1+++ 2title = 'On using reproducible Python development environments' 3date = 2024-11-05 4tags = ['python', 'devenv', 'nix-shell', 'virtualenvs'] 5+++ 6 7One of the things I find annoying about using Python is that there are so many ways of making the development environment of a package or set of scripts reproducible. I have a [repository](https://codeberg.org/hyperreal/admin-scripts) that contains several Python scripts that help me with various tasks, mostly related to qBittorrent. Maybe one or two of these scripts can be separated out into a distinct, individual package, but it's generally more convenient for me to have a directory-level development environment activated with direnv where I can run the other scripts too. I've been using `nix-shell` with a shell.nix file, but this tends to require that I learn the Nix expression language in order to configure shell.nix. I don't like using tools I don't fully understand, and I'm not willing to prioritize learning Nix. It would be nice if I could just use a TOML file to declare everything I need, similar to pyproject.toml, and `nix-shell` would read this instead of shell.nix. As far as I know, the Nix language doesn't have a way to map the TOML structure to Nix expressions. I've tried [devenv.sh](https://devenv.sh/), but this adds extra files and cruft that I don't want polluting my development environment. If I use a plain requirements.txt file with a virtualenv, it can't pull in the Python tools I use, like black, isort, bpython, and pyright. 8 9One solution I'm leaning toward is to install these tools with pipx, where they'd be available to my shell environment relative to my home directory at ~`/.local/bin/`, and use something like 10 11```shell 12uv venv 13source .venv/bin/activate 14uv pip install -r requirements.txt 15echo "layout python3" > .envrc 16direnv allow 17printf ".venv\n.direnv\n" | tee -a .gitignore 18``` 19 20direnv would activate the virtualenv, which in turn would be relative to that git repository directory. 21 22I'm open to suggestions and am curious how others might have solved this problem. Feel free to reach out to me in the Fediverse at [@hyperreal@fedi.hyperreal.coffee](https://fedi.hyperreal.coffee/hyperreal).