How to Create a Python Virtual Environment on Windows

Two Python projects often need different versions of the same package. Install everything into one place and one project eventually breaks another. A Python virtual environment on Windows fixes that by giving each project its own packages, and you can set it up in a few minutes with tools that already ship with Python.
This guide walks through every step with the real output we captured, the exact error PowerShell shows when it blocks activation, and the fix for each common problem. Everything below was checked against the official documentation on November 20, 2026.
Quick answer
If you only need the commands, run these in your project folder:
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install requests
The first line creates the environment, the second activates it in PowerShell, and the third installs a package into it. If PowerShell refuses the second line, jump to the "Running scripts is disabled" fix in the errors section below.
What a virtual environment does
A virtual environment is a folder that holds its own Python interpreter and its own installed packages. The venv documentation describes the folder's pyvenv.cfg file, which records the base Python installation and whether system packages are visible. Because each project has its own folder, upgrading a library in one project cannot change another.
Prerequisites
- Python installed. Check it with
python --version. In our example run on Windows 11 the output wasPython 3.12.10, but any current version of the venv module works the same way. - A terminal. PowerShell or Command Prompt. This guide uses PowerShell.
- A project folder. Open the terminal inside it, or use
cdto move there.
The official Windows guide says the py command is the recommended way to run Python when you have several versions installed, and that it can be used in place of python (Using Python on Windows). If python does not work for you, replace it with py in every command below.
Step 1: Create the environment
Run this in your project folder:
python -m venv .venv
The command prints nothing when it succeeds. It creates a .venv folder, and the name is a convention, not a requirement. This is what the folder looked like in our run:
Inside you will find Include, Lib, Scripts and pyvenv.cfg. The Scripts folder is the one you will use most:
It holds the activation scripts (Activate.ps1 for PowerShell, activate.bat for Command Prompt), pip.exe and the environment's own python.exe.
Step 2: Activate the environment
| Terminal | Command |
|---|---|
| PowerShell | .\.venv\Scripts\Activate.ps1 |
| Command Prompt (cmd.exe) | .\.venv\Scripts\activate.bat |
According to the venv documentation, activation puts the environment's Scripts folder at the front of your PATH and sets the VIRTUAL_ENV variable. You can confirm it worked by checking where python points:
Activation is optional. The same documentation notes that you can run .venv\Scripts\python.exe directly and it will use the environment without activating it. When you finish working, type deactivate.
Step 3: Install packages and save a requirements file
With the environment active, install packages through pip. The venv module installs pip by default through ensurepip (source):
python -m pip install requests
python -m pip freeze
pip freeze prints the installed packages in requirements format, and the pip documentation shows how to save that output to a file with > and reinstall it elsewhere with -r. Installing one package pulled in four more, which is why the list below has five lines:
Save it and rebuild any time:
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt
To confirm pip belongs to your environment and not to the system, run python -m pip --version. The path in the output should contain .venv. If it does not, you are about to install into the wrong place.
Step 4: Use the environment in VS Code
If you work in Visual Studio Code, open the Command Palette and run Python: Select Interpreter, then pick the one inside your .venv folder (VS Code documentation). The same page says the Python extension searches the workspace for a .venv folder and prefers workspace-local environments, so it often finds yours automatically.
Common errors and how to fix them
"Running scripts is disabled on this system"
This is the most common problem on Windows. PowerShell has a safety feature called the execution policy, and when it blocks Activate.ps1 you see this:
The fix suggested in the venv documentation is to allow local scripts for your own account only:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
According to Microsoft's documentation, RemoteSigned lets scripts you wrote locally run and requires a trusted signature on scripts downloaded from the internet, and the CurrentUser scope affects only your account. If a Group Policy manages this setting on your computer, the command cannot override it. In that case use Command Prompt with activate.bat, or call .venv\Scripts\python.exe directly.
python is not found, or it opens the Microsoft Store
The Python documentation lists this in its Windows troubleshooting section. Use the py launcher instead: py -m venv .venv.
The environment stops working after you move the folder
Virtual environments are not designed to be moved or copied. The venv documentation recommends creating a new environment in the new location and reinstalling from a requirements file, which is why Step 3 matters.
A package installed in the wrong place
If import fails after an install, check which Python you used. Run (Get-Command python).Source in PowerShell. When the environment is active, the path should point inside .venv\Scripts, as in the activation image above.
Delete or reset an environment
To remove an environment, run deactivate and delete the .venv folder. Your requirements.txt stays, so you can recreate it at any time. To wipe and rebuild in place, python -m venv --clear .venv deletes the folder's contents first, according to the venv documentation.
Should you commit .venv to Git?
No. Commit requirements.txt and rebuild the environment on each computer. Since Python 3.13, venv also creates a .gitignore inside the folder by default (venv documentation), so Git ignores it automatically on newer versions.
Cheat sheet
| Task | PowerShell command |
|---|---|
| Create | python -m venv .venv |
| Activate | .\.venv\Scripts\Activate.ps1 |
| Install a package | python -m pip install <name> |
| Save the package list | python -m pip freeze > requirements.txt |
| Rebuild from the list | python -m pip install -r requirements.txt |
| Check which pip is used | python -m pip --version |
| Leave the environment | deactivate |
| Reset from scratch | python -m venv --clear .venv |
Frequently asked questions
Do I have to activate the environment every time? No. Activation is a convenience, and running .venv\Scripts\python.exe uses the environment directly (venv documentation).
Where should I create the environment? Inside the project folder is the simplest choice, and it is also where VS Code looks first (VS Code documentation).
Can I rename or move the .venv folder? Environments are not meant to be moved or copied. Create a new one and reinstall from requirements.txt.
Is Set-ExecutionPolicy safe? The CurrentUser scope changes the setting for your account only, and it cannot override a policy set by your organization (Microsoft documentation). Read the page before you change a setting on a work computer.
Conclusion
Create the environment with python -m venv .venv, activate it with Activate.ps1, install with pip, and keep a requirements.txt so you can rebuild it. If PowerShell blocks activation, use the CurrentUser fix above.
Which step gave you trouble? Leave a comment with the exact error message and your Windows version, and we will add the fix to this guide.
Related guide
Keeping project files safe matters as much as setting them up. See How to Back Up Files Automatically on Windows (Free), which uses the built-in robocopy tool and Task Scheduler.
Sources
- venv: Creation of virtual environments, Python documentation, accessed 2026-11-20
- Using Python on Windows, Python documentation, accessed 2026-11-20
- pip freeze, pip documentation, accessed 2026-11-20
- Python environments in VS Code, accessed 2026-11-20
- about_Execution_Policies, Microsoft Learn, accessed 2026-11-20
Comments
Post a Comment