J. Rogers, SE Ohio
If you have been coding for a few years, you probably have a graveyard—or treasure chest—of dozens of repositories sitting on GitHub. Everything from weekend physics simulations and 3D mesh converters to half-finished web servers and IoT experiments.
Then you get a new laptop, clone all 30 repos into a fresh folder, and hit a wall:
- Which version of Python did that project from three years ago use?
- Which repos actually have a
requirements.txt? - How do you set up isolated virtual environments for dozens of projects without spending your entire weekend typing
python -m venv .venvandpip installover and over?
Here is a fast, modern workflow (using
mise and uv) to audit, standardize, and spin up an entire directory of accumulated Python projects cleanly—so every project stays self-contained and reproducible on any machine.Step 1: Give Every Project a
.python-version FileA
requirements.txt file tells Python which packages to install, but it doesn't tell your system which Python interpreter version the code expects. An older script might rely on Python 3.10 behavior, while a newer AI workflow wants Python 3.12 or 3.13.The cleanest standard across modern developer tools is dropping a tiny
.python-version file inside each project root. It is just a plain text file containing a single version number:text
3.11
Use code with caution.
If you use
mise (a modern runtime version manager), you can generate this file and install that exact Python version in one command inside any project folder:bash
mise use python@3.11
Use code with caution.
Now, anytime you (or your tools) enter that folder, your environment immediately knows which Python version belongs there.
Step 2: Auto-Generate Missing
requirements.txt FilesIf you have older weekend projects that never got a
requirements.txt, you don't need to manually read through dozens of .py files to figure out what you imported.You can use
pipreqs via uvx (which runs the tool temporarily without cluttering your global system) to scan a project's actual import statements and generate a clean requirements.txt automatically:bash
cd ~/projects/MyOldProject
uvx pipreqs .
Use code with caution.
Do a quick sanity check on the generated
requirements.txt to make sure package names look right (for example, ensuring cv2 mapped to opencv-python or PIL mapped to pillow), and commit both .python-version and requirements.txt to your repo. Once those two files live in Git, setting up that repo on any future computer takes seconds.Step 3: Batch-Build Virtual Environments Across All Projects at Once
Once your projects have
.python-version and requirements.txt files, you don't need to set up virtual environments one by one. Using uv (an ultra-fast Rust-based Python package and environment manager), you can loop through your entire projects folder and build every single .venv in one shot.Run this from the parent directory holding all your repositories:
bash
for dir in */; do
if [ -f "$dir/requirements.txt" ]; then
echo "=== Setting up $dir ==="
(cd "$dir" && uv venv && uv pip install -r requirements.txt)
elif [ -f "$dir/pyproject.toml" ]; then
echo "=== Syncing $dir ==="
(cd "$dir" && uv sync)
fi
done
Use code with caution.
What this loop actually does:
- Enters each folder safely: Wrapping the command in
(cd "$dir" && ...)runs it in a subshell so it never loses its place in your parent directory. - Reads
.python-versionautomatically:uv venvdetects the.python-versionfile inside each folder, fetches that exact Python interpreter if needed, and creates a standalone.venv/directory. - Installs dependencies at warp speed:
uv pip installresolves and installs packages 10–50x faster than traditionalpip, keeping every project completely isolated. - Skips non-Python repos: Any C, Rust, or Go folders without a
requirements.txtorpyproject.tomlare safely skipped.
Step 4: Make
.venv Activate Automatically When You cdHaving 30 isolated
.venv folders is great, until you get tired of typing source .venv/bin/activate every time you jump between projects.You can automate that too. Add this lightweight hook to your
~/.bashrc or ~/.zshrc:bash
auto_venv() {
if [[ -n "$VIRTUAL_ENV" && "$PWD" != "$(dirname "$VIRTUAL_ENV")"* ]]; then
deactivate
fi
if [[ -z "$VIRTUAL_ENV" && -f ".venv/bin/activate" ]]; then
source .venv/bin/activate
fi
}
cd() {
builtin cd "$@" && auto_venv
}
Use code with caution.
Now, the moment you
cd into any project folder, its isolated virtual environment activates automatically—and deactivates the second you leave.The Payoff
Spending 20 minutes adding
.python-version and requirements.txt to your accumulated projects turns a messy folder of "stuff that only worked on my old laptop" into a clean, portable portfolio. Next time you switch machines, spin up a server, or revisit a project two years later, everything works on the first run.
No comments:
Post a Comment