2 min read

Creating Python Virtual Environments on Windows 11

This guide details how to set up a Python virtual environment specifically on Windows 11, utilizing the modern Windows Terminal and PowerShell.

Prerequisites

Ensure Python is installed and added to your PATH.

  1. Open Terminal (PowerShell).
  2. Type python --version. If you see a version number, you are ready.

Step 1: Open Your Project Folder

Windows 11 makes this easy:

  1. Navigate to your project folder in File Explorer.
  2. Right-click anywhere in the empty space.
  3. Select Open in Terminal.

Step 2: Create the Virtual Environment

Run the following command to create a virtual environment named .venv (a common convention).

python -m venv .venv

This creates a folder named .venv containing a copy of the Python interpreter and a Scripts folder.

Step 3: Activate the Environment

In Windows 11, the default terminal uses PowerShell. To activate the environment:

.\.venv\Scripts\Activate.ps1

Troubleshooting: Execution Policy Error

If you see an error in red text saying "running scripts is disabled on this system", you need to change your execution policy. This is a default security feature in Windows.

To allow scripts for the current user only (safe):

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Then try running the activate command again.

Step 4: Verify Activation

Once activated, you should see (.venv) at the start of your command prompt line. You can also verify by checking which python is being used:

where.exe python

It should point to the python executable inside your .venv folder, not the system one.

Step 5: Install Packages

Now you can install packages safely isolated from your main system.

pip install requests

programming/python/python