How to Set Up a Python Virtual Environment on Windows 11: A Beginner's Guide

Programming September 12, 2026
How to Set Up a Python Virtual Environment on Windows 11: A Beginner's Guide

Ten minutes of setup now saves you an afternoon of confusing errors later. If you have ever installed a Python package, watched a different project break, and had no idea why, a virtual environment is the fix — and on Windows 11 it takes about three commands.

The idea is simple: instead of letting every library land in one shared Python installation, you give each project its own private set of packages. Nothing leaks out, nothing collides. Here is how to do it from scratch.

Why a virtual environment is worth the trouble

You do not need one for a throwaway script that uses only the standard library. You do need one the moment two projects want different versions of the same library, which happens sooner than most people expect.

Without isolation, every install writes into the Python installation attached to your whole user account. Project A needs an older version of a library; project B needs the newest. Installing for B quietly upgrades A's dependency, and the next time you run A, something breaks with a traceback that mentions none of your own code.

A virtual environment is just a folder. Inside it you get a private copy of the Python interpreter, its own Scripts and Lib directories, and its own package list. While it is active, python and pip point inside that folder. While it is not, they point at your global installation, which stays untouched.

Two extra benefits: the environment doubles as a record of what a project needs, and deleting it is as easy as deleting the folder.

Check Python is installed and reachable

Open Windows Terminal or PowerShell — press Win + X and choose Terminal. Type this and press Enter:

python --version

If you see something like Python 3.12.4, you are set. If Windows answers with an error, try the launcher instead: py --version.

If both fail, install Python from python.org and tick "Add python.exe to PATH" on the first screen of the installer. The Microsoft Store version works too and handles updates for you; either is fine here.

One Windows quirk worth knowing: if typing python opens the Microsoft Store instead of printing a version, you have hit the built-in app execution alias. Either install Python properly, or turn the alias off under Settings > Apps > Advanced app settings > App execution aliases, where you can switch off the python.exe and python3.exe entries.

Finally, confirm pip is present with python -m pip --version. If that prints a version number, you have everything you need.

Create the environment

Move into your project folder. If you do not have one yet, create an empty folder so you can follow along:

cd C:\Users\YourName\Projects\my-project

Then create the environment:

python -m venv .venv

Use python -m venv rather than the older virtualenv tool; it ships with Python, so there is nothing extra to install. The final argument is the folder name. Calling it .venv is a convention that editors such as VS Code recognise automatically, but venv or env work just as well.

After a few seconds you will have a new .venv folder containing a small Scripts directory, a Lib directory and a pyvenv.cfg file. Do not edit anything inside it by hand. If you ever make a mess, delete the whole folder and create it again — that is the entire repair procedure.

Activate it before you install anything

An environment does nothing until it is active. In PowerShell, run:

.\.venv\Scripts\Activate.ps1

In Command Prompt, use the batch file instead:

.venv\Scripts\activate.bat

When it works, your prompt gains a prefix — (.venv) — which tells you that python and pip now refer to the project's own interpreter. You can double-check with where python; the first result should be a path inside your project folder.

If PowerShell refuses to run the script

You may see a message about scripts being disabled on this system. That is PowerShell's execution policy doing its job, and the usual fix is to allow locally written scripts for your own account only:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Run that in PowerShell, confirm with Y, and try again. If the machine is managed by an employer, ask IT first rather than changing policy settings yourself.

Leaving the environment

When you are finished, type deactivate. The prompt loses its prefix and you are back to your global Python. Closing the terminal does the same thing.

Install packages inside the environment

With (.venv) showing in your prompt, install what you need:

python -m pip install requests

Two habits are worth forming now. First, always check the prompt before installing — that prefix is the difference between a tidy project and a broken global Python. Second, prefer python -m pip over a bare pip. It guarantees you are installing into the interpreter you think you are using, which matters on a machine with several versions of Python installed.

Useful commands while you are in there:

  • python -m pip list shows everything installed in the environment.
  • python -m pip show requests prints details about one package, including its version and location.
  • python -m pip install --upgrade requests updates a package to a newer release.
  • python -m pip uninstall requests removes it again.

One naming tip: if you have a file called requests.py in your project folder, your own file will shadow the installed library and produce confusing import errors. Avoid naming scripts after popular packages.

Record what the project needs

While the environment is active, run:

python -m pip freeze > requirements.txt

That writes a list of installed packages and their exact versions into requirements.txt. Keep that file with your code, and anyone — including future you on a new laptop — can rebuild the setup with:

python -m pip install -r requirements.txt

Add a .gitignore file containing .venv/Photo: StockSnap / Pixabay