Chapters
Python114 chapters

Beyond the basicsChapter 88 of 114

Virtual Environments

Give every project its own set of packages, and stop them fighting.

The problem they solve

Install packages into the system Python and every project shares them. Project A needs version 1 of a library, project B needs version 2, and only one can win. Worse, a system upgrade can break tools your operating system depends on.

A virtual environment is a folder holding its own interpreter and its own packages. One per project, and the conflict disappears.

Making one

bash
cd my-project
python -m venv .venv

That creates a .venv folder. The name is a convention; editors look for it.

Activating

bash
source .venv/bin/activate          # macOS and Linux
.venv\Scripts\activate             # Windows, cmd or PowerShell

Your prompt gains a (.venv) prefix. From then on python and pip mean the ones inside the environment:

bash
python -m pip install requests
python -m pip list
deactivate

deactivate puts everything back.

Checking which Python you are on

Python
import sys
print(sys.prefix != "" and sys.base_prefix != "")
print(sys.prefix == sys.base_prefix)

Output

True
True

Inside an active environment those two differ, so the second line prints False. On this page there is no environment, so they are equal. It is the quickest way to answer "am I actually in my venv?".

Recording what you need

bash
python -m pip freeze > requirements.txt

Someone else — or you on another machine — then rebuilds it exactly:

bash
python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt

Do not commit it

Add this to .gitignore:

text
.venv/

The folder holds hundreds of megabytes of platform-specific binaries. The requirements.txt is the part worth keeping in version control: it is the recipe, not the meal.

Common problems

"I installed it but the import fails." You installed into a different interpreter. Use python -m pip install, not bare pip, and check sys.executable.

The editor cannot see the packages. Point it at the environment's interpreter. In VS Code that is the Python: Select Interpreter command.

Activation fails on Windows PowerShell. The execution policy blocks the script:

bash
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

You do not always have to activate

Running the environment's interpreter directly works and is handy in scripts:

bash
.venv/bin/python -m pip install requests
.venv/bin/python main.py

Newer tools

uv and poetry do the same job with lockfiles and faster installs, and uv in particular has become popular:

bash
uv venv
uv pip install requests

They are worth knowing about. venv is built in, works everywhere, and is enough to start.

Test yourself

2 questions

What problem does a virtual environment solve?

Show the answer

Two projects needing different versions of the same package — It also keeps you from breaking tools your operating system depends on.

Should .venv go into version control?

Show the answer

No; commit requirements.txt instead — The folder holds platform-specific binaries. The requirements file is the recipe rather than the meal.

Next chapter

Type Hints

Say what types you expect, for readers and for tools.