Create a Python virtual environment on Windows and check it works

Give one local Python project its own environment, verify its Python and pip paths, and run a script without changing PowerShell security settings.

Beginner5 min read
In this guide

A virtual environment gives one project its own place for Python packages. Installing a package there does not add it to every other project using your normal Python installation.

Here you will create an environment named .venv, check that both Python and pip belong to it, and run a small script with it. Activation is optional. Using the environment’s Python directly also avoids the common PowerShell “running scripts is disabled” problem.

This is a local Windows PowerShell guide using Python 3.12 or newer. It does not require a server or administrator access.

Start in a project folder

Create a folder named python-project somewhere you can write files. Open it in File Explorer, type powershell in the address bar, and press Enter. The terminal now starts in that folder.

Check Python:

py --version

You should see Python 3.12.x or a newer version. If py is not recognized, install Python using the official Windows downloads, reopen PowerShell, and retry. The official installer/manager options vary; do not continue until the version command works.

For an existing project, open its actual folder instead. If it already has a working .venv, check that environment in the next section rather than recreating it. Do not reuse this disposable example over someone else’s environment.

Create the environment and verify its Python

Run:

py -m venv .venv

-m venv tells Python to run its built-in environment creator. A successful command usually returns to the prompt without a message. Look for a new .venv folder; it holds the environment, not your source code.

Now run its interpreter—the program that executes Python—and its package installer:

.\.venv\Scripts\python.exe -c "import sys; print(sys.executable); print(sys.prefix != sys.base_prefix)"

After this succeeds, check pip with that same interpreter:

.\.venv\Scripts\python.exe -m pip --version

The first command should show a path ending in python-project\.venv\Scripts\python.exe and then True. The pip output should also point inside .venv. Your full folder path and pip version will differ; the important part is the same project environment in both paths.

If creation fails, stop before these checks. Confirm that Python is installed normally and the folder is writable. A half-created .venv is not evidence of a working environment.

Run a script with that interpreter

If you are using the small example, save this UTF-8 plain-text file as hello.py beside .venv, not inside it. In Notepad, choose All files to avoid hello.py.txt. If you already have your own script, keep that file and skip creating hello.py.

import sys

print("The project script is running.")
print(f"Python: {sys.executable}")

For an existing project, first check its package instructions. If it provides a requirements.txt file, review it and install those packages into this environment:

.\.venv\Scripts\python.exe -m pip install -r requirements.txt

Skip installation for the hello.py example; it needs no packages. Package installation downloads and runs third-party code, so use requirements from a project you trust. Continue only after installation finishes successfully.

Now run the script. For the example, use the command below. For your own script, replace hello.py with its actual filename and add its required arguments before running the command:

.\.venv\Scripts\python.exe hello.py

The example should show the message and the same .venv Python path. For your own script, check its documented output instead. Creating an environment does not copy your script or automatically install its packages.

Optional: activate it for shorter commands

Activation adjusts the current terminal so python means this environment’s interpreter. It does not turn the environment into a background service.

.\.venv\Scripts\Activate.ps1

If activation works, check python -c "import sys; print(sys.executable)" before relying on the shorter command. The path must still point to .venv.

If PowerShell blocks the activation script, keep using .\.venv\Scripts\python.exe. You do not need to disable the security policy to run your project this way.

After successful activation, deactivate restores the terminal’s previous path. Closing the terminal also ends activation. If you never activated, there is nothing to deactivate.

Keep the environment disposable

Keep your script, data, and requirements outside .venv. Do not copy the environment to another computer or commit it to Git; recreate it there with an installed compatible Python version and the project’s package requirements.

For the disposable example, you can close PowerShell and remove only the .venv folder in File Explorer once you have confirmed it contains no personal files. That removes its packages, not hello.py. In an existing project, first check its setup instructions and preserve any requirements needed to rebuild it.

You now know which Python runs this project. A virtual environment does not keep a script running after you close a terminal; that is a separate task covered in the continuous VPS script guide.

  • Python venv documentation — explains creation, activation, direct interpreter use, and why environments should be recreated rather than moved.
  • Python ensurepip — reference for the pip bootstrap used during normal environment creation.
  • pip user guide — explains installing requirements with the intended Python interpreter.
← Back to servers & scripts guides