Run python3 -m venv .venv on Linux/macOS (or py -3 -m venv .venv on Windows via the Python Launcher) to create an isolated Python 3 virtual environment inside a .venv folder, and activate it (source .venv/bin/activate on POSIX or .venv\Scripts\activate on Windows) before installing packages with python -m pip install.
Basic Example
# --- macOS / Linux ---
# 1. Recommended: Create the virtual environment with Python 3
python3 -m venv .venv
# 2. Activate the environment:
source .venv/bin/activate
# 3. Verify Python 3 and upgrade pip inside the isolated environment:
python --version
python -m pip install --upgrade pip
# --- Windows (PowerShell / Command Prompt) ---
# 1. Recommended: Use the `py -3` launcher to target Python 3
py -3 -m venv .venv
# 2. Activate (PowerShell):
.venv\Scripts\Activate.ps1
# Or Command Prompt (cmd.exe):
# .venv\Scripts\activate.bat
Key Details & Edge Cases
- Targeting an exact Python 3 minor version (e.g., Python 3.12 vs. 3.13):
- If multiple Python 3 versions are installed side by side, invoke the specific binary directly so the virtual environment binds to that exact interpreter:
- macOS / Linux:
python3.12 -m venv .venv - Windows:
py -3.12 -m venv .venv uv(modern ultra-fast alternative):uv venv --python 3.12 .venv(can also automatically download Python 3.12 if missing).
- macOS / Linux:
- If multiple Python 3 versions are installed side by side, invoke the specific binary directly so the virtual environment binds to that exact interpreter:
- Debian / Ubuntu
ensurepip is not availableerror:- Minimal Debian and Ubuntu installations omit
ensurepipfrom the basepython3package, causingpython3 -m venv .venvto fail withThe virtual environment was not created successfully because ensurepip is not available. Install the matching versioned venv package first:sudo apt update && sudo apt install -y python3-venv # Or for a specific version: sudo apt install -y python3.12-venv
- Minimal Debian and Ubuntu installations omit
- Windows PowerShell
ExecutionPolicyerror onActivate.ps1:- If PowerShell blocks activation with
running scripts is disabled on this system, either allow local scripts for your current user (Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser) or bypass activation altogether by running the environment’s executables directly (.venv\Scripts\python.exe -m pip install requests).
- If PowerShell blocks activation with
- Always use
python -m venv, never legacyvirtualenvorpyvenv:pyvenvwas deprecated in Python 3.6 and removed in Python 3.8. Built-invenvships with every Python 3.3+ standard library without third-party dependencies.
- Keep
.venvout of Git (echo ".venv/" >> .gitignore) and never move/rename the folder:- Virtual environments hardcode absolute interpreter paths in
pyvenv.cfgandbin/activatescripts; if you rename or move your project directory, delete.venv(rm -rf .venv) and recreate it cleanly fromrequirements.txtorpyproject.toml.
- Virtual environments hardcode absolute interpreter paths in
Comparison of Python 3 Virtual Environment Creation Commands
| Command | Platform | Built-in (stdlib)? |
Pins Specific Python 3 Version? | Best Used When |
|---|---|---|---|---|
python3 -m venv .venv (Recommended on POSIX) |
macOS / Linux | Yes | Uses default python3 (python3.12 -m venv pins exact version) |
Standard Python 3 project setup on Linux, macOS, and WSL. |
py -3 -m venv .venv (Recommended on Windows) |
Windows | Yes | Uses latest Python 3 (py -3.12 -m venv pins exact version) |
Standard Python 3 project setup on Windows regardless of PATH order. |
uv venv --python 3.12 .venv |
Cross-platform | No (requires uv) |
Yes (even downloads missing Python 3 versions automatically) | High-speed modern workflows (10x–100x faster env creation and package resolution). |
conda create -n myenv python=3.12 |
Cross-platform | No (requires Conda/Miniforge) | Yes | Data science / C-extension stacks requiring non-Python binary libraries (CUDA, GDAL, MKL). |
You must log in or register to comment.
