bhylu.com

Advertisements

Python VirtualEnv

A virtual environment gives one project its own installed packages. It helps avoid changing unrelated projects when a dependency needs another version.

Understand the idea

On Windows, create .venv with py -m venv .venv. You can run its interpreter directly without activation. Activation only adjusts the current shell's PATH and prompt; it is not required for the environment to work. Keep .venv out of source control and record dependencies instead of copying the environment folder between computers.

Where to try it

Run these commands in a local Windows terminal, not inside a Python source file. They require a local Python installation. The online text runner does not provide an operating-system shell.

A worked example

Windows terminal

py -m venv .venv
.venv\Scripts\python.exe -m pip --version

Read the result

Run these in the project terminal. The second command should report pip from the newly created environment.

Try one change

Run .venv\Scripts\python.exe -c "import sys; print(sys.executable)" and verify the interpreter location.