Photo by 𝓢𝓱𝓪𝓷𝓮 𝓦𝓮𝓼𝓽 ™: https://www.pexels.com/photo/red-vintage-car-cruising-on-open-road-at-sunset-29967816/

Step One

User runs command to create a virtual environment.

Code snippet in terminal 'python -m venv .venv'
Photo by Nick Wehrli: https://www.pexels.com/photo/aerial-view-of-a-road-in-the-desert-5717798/

Step Two

PowerShell resolves python via the PATH and finds python.bat in \.pyenv\pyenv-win\shims\. Because it’s a .bat, Windows runs it under cmd.exe.

Left image description
Right image description
Photo by BOOM 💥: https://www.pexels.com/photo/people-holding-a-stick-on-two-ends-12585927/

Step Three

PowerShell asks Windows to start running the shim file python.bat. Windows launches cmd.exe to do that work, since .bat files are read line-by-line by the command processor. From there, the shim calls pyenv, which decides which installed Python version to use. In this case, since no Python version has been set locally or in shell-space, Windows spawns the globally-set pyenv Python version, found in .pyenv\pyenv-win\version. At the end of this step, a new Python process starts—the one chosen by pyenv.

Left image description
Right image description
Photo by Joshua Welch: https://www.pexels.com/photo/bird-s-eye-view-of-roadway-surrounded-by-trees-1624600/

Step Four

The newly started Python process runs the built-in venv module. It creates a folder named .venv, copies or links a small Python executable into it, adds the usual Scripts/ and Lib/ subfolders, and writes a small configuration file named pyenv.cfg. That file tells future Python runs where the base installation

Top image description
Bottom image description
Photo by Kelly : https://www.pexels.com/photo/aerial-view-of-richmond-skyline-and-bridge-34586196/

Step Five

When you later activate the environment (.\.venv\Scripts\activate), the shell simply updates its PATH so that .venv\Scripts appears first. Now, any time you type python, the shell finds the copy inside .venv before anything else. From that point on, every Python command uses the isolated environment you just created.

-Code snippet in terminal 'python -m venv .venv'

Related Posts

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

pyenv and venv: a buddy roadtrip movie