Python Environment Cheat Sheet
For a quite long time I have being a tooler of development environments, even when I just want to do a script or an experiment I usually will do on a isolated environment, with control and the minimum variables and packages as I can. This helps a lot of things but the most important for me is the reproducibility. One of the things I most dislike on software development is the classical: “Works on my machine/setup”. Even if you pack everything on a docker image or something alike, if you can not easily rebuild that still working only on your machine.
Bootstrap
We need to create a local sane environment to install and run stuff and currently I am using uv to set it. You can use the regular pip method but for now since it is a Rust application, I would just go by the Rust way:
# Installing with cargo direct from github, it will compile and install a lot of things
# first and may take a while depending on your machine.
cargo install --git https://github.com/astral-sh/uv uv
> which uv
/home/$user/.cargo/bin/uv
Then having uv installed we create a virtual environment. I normally have one big generic uv that I have at my home dir, usually named generic-venv and then one per project if it is necessary. I create the virtual environment using:
uv venv .venv
To use it on the current terminal you just activate it as usual:
source .venv/bin/activate
Usual Tasks
Daily mundane tasks, most of them are just prefixing uv on the typical pip command:
# Installing packages:
uv pip install <package-name>
# Freezing the packages on the current environment:
uv pip freeze > requirements.txt
# Installing the package list on the current environment
uv pip install -r requirements.txt