The Py Hub

Python virtual environments: a quick guide

Guides4 min read20 October 2026

By · Published

What Python virtual environments are and why you need them, how to create and activate one on Windows, macOS and Linux, managing packages with pip and requirements files, the 'externally managed environment' error, and newer tools.

A virtual environment is a self-contained folder holding a Python interpreter and its own installed packages. It keeps each project's dependencies separate, so upgrading a library for one project doesn't break another, and you never have to install packages into your system Python.

Why bother?

Create one

Python includes the venv module. In your project folder:

python3 -m venv .venv     # macOS/Linux
py -m venv .venv          # Windows

This creates a .venv folder. The name is a convention; many editors detect it automatically.

Activate it

source .venv/bin/activate        # macOS/Linux (bash, zsh)
.venv\Scripts\activate           # Windows Command Prompt
.venv\Scripts\Activate.ps1       # Windows PowerShell

Your prompt usually shows (.venv). Now python and pip refer to the environment. Type deactivate to leave. If PowerShell blocks the script, you may need to adjust its execution policy for your user; check your organisation's rules first.

You don't have to activate: you can run .venv/bin/python script.py directly, which is handy in scripts and scheduled jobs.

Install packages

python -m pip install requests
python -m pip list

Using python -m pip guarantees you're using the pip that belongs to the active interpreter.

Record and reproduce dependencies

python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt

Commit requirements.txt to version control, but add .venv/ to .gitignore: environments aren't portable between machines, so recreate them instead of copying them.

The "externally managed environment" error

On recent versions of Debian, Ubuntu, Fedora and Homebrew Python, running pip install outside a virtual environment may fail with an externally-managed-environment error. This comes from PEP 668: the operating system manages that Python, and pip installs could break system tools. The fix is to create a virtual environment, or use your system package manager or pipx for command-line applications. Avoid overriding the protection.

Choosing a Python version

A venv uses the Python that created it. To use a different version, create the environment with that interpreter, for example python3.13 -m venv .venv, or on Windows py -3.13 -m venv .venv. Check the Python release status page and use a version that still receives security fixes.

Newer tools

All build on the same idea. Pick one per project and stick with it.

Troubleshooting

Editor setup

In VS Code, open the Command Palette and choose "Python: Select Interpreter", then pick the .venv interpreter. PyCharm offers to create one for new projects.

Frequently asked questions

Can I move or rename a venv?

Not reliably, as paths are hard-coded. Delete it and recreate it from requirements.txt.

Do I need one for a tiny script?

Not if it uses only the standard library. As soon as you install packages, use one.

Sources and further reading

Facts checked on 20 October 2026. Spotted an error? Tell us and we will correct it.

Get the next guide first

New articles land every week. Follow along by RSS or bookmark the homepage.

Follow by RSS