Creating and Activating Virtual Environments with venv
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Showing steps for Windows. They are marked “On Windows” below.
Python comes bundled with the venv module. You do not need to install any external tools to create isolated, project-specific environments.
Creating a Virtual Environment
venv is the module in Python's standard library that creates a virtual environment. python -m venv .venv builds a folder named .venv inside your project.
Go to your project folder, then ask Python to build the environment. The name .venv is the usual choice. It creates a hidden folder with pyvenv.cfg, an interpreter link and an empty site-packages.
On Windows
In PowerShell, use the py launcher. After a moment the folder appears.
PS C:\Users\Priya> cd shop-appPS C:\Users\Priya> py -m venv .venvPS C:\Users\Priya> dir .venvOutput Directory: C:\Users\Priya\shop-app\.venv Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 03-11-2026 09:21 Include d----- 03-11-2026 09:21 Lib d----- 03-11-2026 09:21 Scripts -a---- 03-11-2026 09:21 123 pyvenv.cfg
Activating the Environment
Creating the folder is not enough. Activating it tells your shell to use the environment's Python and pip first. The prompt then starts with (.venv).
On Windows
PowerShell runs Activate.ps1. If it says running scripts is disabled, allow local scripts once for your user, then try again. In Command Prompt, run .venv\Scripts\activate.bat instead.
PS C:\Users\Priya\shop-app> .venv\Scripts\Activate.ps1PS C:\Users\Priya\shop-app> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserPS C:\Users\Priya\shop-app> .venv\Scripts\Activate.ps1Output.venv\Scripts\Activate.ps1 : File C:\Users\Priya\shop-app\.venv\Scripts\Activate.ps1 cannot be loaded because running scripts is disabled on this system. (.venv) PS C:\Users\Priya\shop-app>
Verifying the Active Interpreter
Before you install anything, check which Python is running. The path should point inside your project's .venv folder, not the system folder.
On Windows
Get-Command shows where PowerShell finds python. The path ends in .venv\Scripts\python.exe.
(.venv) PS C:\Users\Priya\shop-app> Get-Command pythonCommandType Name Version Source ----------- ---- ------- ------ Application python.exe 3.12.4150 C:\Users\Priya\shop-app\.venv\Scripts\python.exe
Deactivating When Finished
Deactivate is the command that leaves a virtual environment. It restores your shell's PATH so python and pip point to the system installation again.
When you finish working on the project or want to switch to another project, restore your shell’s original state.
deactivateThe (.venv) prefix disappears, and your shell reverts to using the global Python configuration.
Example
import os
virtual_env = os.environ.get("VIRTUAL_ENV")if virtual_env: print(f"Active venv path: {virtual_env}")else: print("No VIRTUAL_ENV environment variable detected")No VIRTUAL_ENV environment variable detected
What this code does
- When you activate a virtual environment, the shell sets the
VIRTUAL_ENVenvironment variable. - Your scripts and tools (like VS Code and pytest) use this variable to detect the active environment.
Next step
Check your understanding
7 quick questions