Programming & Coding How-TosTechnology

How to Create and Activate a Python Virtual Environment on Windows 11

Admin
How to Create and Activate a Python Virtual Environment on Windows 11

Every experienced Python developer gives the same advice to beginners on Windows 11: use a virtual environment for each project. It sounds like extra ceremony until the first time two projects need different versions of the same library and break each other. A virtual environment gives each project its own private Python setup, and creating one takes less than a minute. This guide shows you the exact commands for Windows 11 in PowerShell, Command Prompt, and VS Code, plus the fixes for the activation errors almost everyone hits the first time.

Quick Answer

Open a terminal inside your project folder and run py -m venv .venv to create the environment. Activate it in PowerShell with .venv\Scripts\Activate.ps1 or in Command Prompt with .venv\Scripts\activate.bat. Your prompt will show (.venv) when it is active. Install packages with pip install while it is active, and type deactivate when you finish.

What a Virtual Environment Actually Is

A virtual environment is a folder that contains a copy of the Python interpreter setup, its own pip, and its own package library folder. When the environment is active, the commands python and pip point at that folder instead of your global installation. Packages you install go into the project folder and nowhere else, so deleting the folder removes everything cleanly.

The benefits are practical. Project A can pin an old version of a library while Project B uses the newest one. Nothing you install can break Windows system tools or other projects. And when you share your code, a requirements.txt file lets anyone rebuild the same environment in minutes. The official venv documentation covers the module in full if you want every option.

Step 1: Check Python Is Installed

  1. Open Windows Terminal, PowerShell, or Command Prompt.
  2. Run py --version. You should see something like Python 3.12.x.
  3. If the command fails or the Microsoft Store opens instead, install Python from python.org first and tick Add python.exe to PATH during setup. Our guide to fixing pip not recognized in Windows Command Prompt covers that repair in detail.

Step 2: Create the Environment

  1. Move into your project folder, for example cd C:\Users\YourName\Projects\weather-app. Create the folder first if it does not exist.
  2. Run py -m venv .venv. The name .venv is a widely used convention that tools like VS Code detect automatically. The command takes a few seconds and prints nothing when it succeeds.
  3. Look inside the folder. You will see a new .venv folder containing Scripts, Lib, and a pyvenv.cfg file. That folder is the entire environment.

Step 3: Activate the Environment

Activation adjusts your current terminal session so that python and pip use the environment. The command depends on your shell:

  • PowerShell: .venv\Scripts\Activate.ps1
  • Command Prompt: .venv\Scripts\activate.bat
  • Git Bash: source .venv/Scripts/activate

After activation, your prompt starts with (.venv). Confirm it worked by running where python in Command Prompt or Get-Command python in PowerShell. The first path shown should point inside your project’s .venv folder.

Fixing the PowerShell Execution Policy Error

On many Windows 11 machines, the first activation attempt fails with a red error saying the script cannot be loaded because running scripts is disabled on this system. Windows is blocking unsigned scripts by default. Fix it once for your user account with this command:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

Type Y and press Enter when asked. This setting allows local scripts like the activation script to run, while still blocking unsigned scripts downloaded from the internet. Run the activation command again and it will succeed. You never need to lower the policy machine-wide or run the terminal as administrator for normal project work.

Step 4: Install Packages and Save Them

  1. With the environment active, install packages normally: pip install requests.
  2. Save the exact list for later: pip freeze > requirements.txt.
  3. On another machine, or after deleting the environment, recreate it and run pip install -r requirements.txt to restore everything at the same versions.

Using the Environment in VS Code

Open the project folder in VS Code and it usually detects .venv automatically. If not, press Ctrl+Shift+P, choose Python: Select Interpreter, and pick the entry whose path contains .venv. New terminals opened inside VS Code will then activate the environment automatically, and the Run button will use it too.

Common Mistakes to Avoid

  • Creating the environment inside OneDrive or a synced folder. Sync tools can corrupt the many small files inside .venv. Keep projects in a plain local folder such as C:\Projects.
  • Committing .venv to Git. The folder is large and machine-specific. Add .venv to your .gitignore and share requirements.txt instead.
  • Installing packages before activating. If the prompt lacks (.venv), the packages go to global Python and the project still fails.
  • Moving the project folder after creating the venv. Activation scripts contain absolute paths. If you move the project, delete .venv and create it again.

Day-to-Day Workflow With Your Environment

Creating the environment is a one-time act; using it becomes a daily rhythm. Each time you sit down to work on the project, open a terminal in the project folder and run the activation command before anything else. Make it a reflex: new terminal, activate, then work. When you install a new package mid-project, immediately refresh requirements.txt with pip freeze so the file never drifts away from reality.

A few habits smooth the routine further. In VS Code, once the interpreter is set to the project’s .venv, integrated terminals activate automatically, removing the manual step entirely for editor-based work. If you open a second terminal window for a different project, activate that project’s environment in that window; each terminal session carries its own activation state, and confusing the two is a common source of “my packages vanished” moments that are really “wrong window” moments. When a project is finished or its environment becomes a tangle after months of experiments, do not try to clean it: delete the .venv folder, create a fresh one, and reinstall from requirements.txt. A clean rebuild takes a couple of minutes and is one of the great luxuries environments give you — a Python setup you can throw away and recreate on demand, with zero fear.

Conclusion

On Windows 11 the whole routine is three commands: py -m venv .venv to create, the Activate.ps1 or activate.bat script to enter, and deactivate to leave. Fix the execution policy once, select the environment in VS Code, and each of your projects gets a clean, private Python that can never clash with the others.

Frequently Asked Questions

Do I need a separate virtual environment for every project?

Yes, that is the point of them. Each project gets its own environment folder, usually named .venv inside the project. They are cheap to create and prevent version conflicts completely, so there is no benefit in sharing one environment across unrelated projects.

Why does my prompt not show (.venv) after activation?

Either the activation script did not run, or you are looking at a different terminal window. Activation applies only to the current session. Run the activate command in the exact window you are working in, and check for errors above the prompt, especially the PowerShell execution policy message.

Can I delete a virtual environment safely?

Yes. Deactivate it, then delete the .venv folder like any other folder. Your project code is untouched. Recreate the environment at any time with py -m venv .venv and restore packages from requirements.txt.

What is the difference between venv and virtualenv?

venv is built into Python 3.3 and later, so it needs no installation and is the recommended choice today. virtualenv is an older third-party tool with extra features and slightly faster creation. For learning and everyday projects, the built-in venv is all you need.

How do I know which Python version the environment uses?

Run python --version while the environment is active. The environment inherits the version of the interpreter that created it. To build an environment with a specific version on Windows, name it at creation, for example py -3.11 -m venv .venv if Python 3.11 is also installed.

Admin

Passionate about the intersection of style and storytelling. I simplify fashion trends and industry news into easy-to-follow advice, helping you stay stylish and informed in the fast-paced world of fashion.

One response to “How to Create and Activate a Python Virtual Environment on Windows 11”

Leave a Reply

Your email address will not be published. Required fields are marked *