Toast spread with peanut butter labeled pyenv and jam labeled venv.
Photo by Karola G: https://www.pexels.com/photo/spreading-of-jam-on-toast-6659871/ Memefied by Eli Kent

TL;DR: Digging deep into how pyenv and venv redirect Python calls helped me get a tactile sense of my environment and understand how developers use environment variables and shims to stitch programs together and build flexible, robust environments.

What I did

A deep dive into venv and pyenv.

Why I did it

I knew pyenv and venv are essential tools for building robust Python applications, but my understanding of how they worked was fuzzy. Level-up means water shoes on, gloves on, into the weeds.

What I learned

Both tools depend on the same mechanism — the PATH — but use it for different layers of control.

pyenv

Purpose: Controls which Python version is used globally, per shell, or per project.

How it works: Inserts the directories ...\pyenv-win\shims\ and ...\pyenv-win\bin\ into the PATH environment variable. When you run python, PowerShell resolves to the shim (python.bat) in shims\, which applies pyenv’s version resolution (local → shell → global) and launches the selected interpreter.

Note: Ensure this shim directory appears in PATH before any other directory that contains a Python executable.

venv

Purpose: Provides an isolated environment for packages and execution.

How it works:

  1. Builds the .venv directory structure.
  2. Writes pyvenv.cfg with the base interpreter information.
  3. When activated, prepends .\.venv\Scripts\ to PATH, so python resolves to .\.venv\Scripts\python.exe.

Note: The python.exe inside .venv is a lightweight bootstrap that reads pyvenv.cfg and runs the real interpreter (e.g., in ...\pyenv-win\versions\...).

How they work together

With pyenv managing your installed versions, any virtual environment you create inherits that context. Inside the venv, python resolves to .venv\Scripts\python.exe, which ultimately runs the specific interpreter version that pyenv selected.

Takeaway

I noticed two compsci meta-patterns:

  1. Use environment variables (especially PATH) to route execution to the right directory.
  2. Use shims or bootstrap executables to redirect calls without hard-coding paths.

I like how both tools solve the same navigational problem at different layers of the stack.

tags: #Python, #PowerShell, #applied-learning, #fundamentals

pyenv and venv work together like Peanut Butter and Jelly