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 in 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 is active and upgrade pip:
python --version
python -m pip install --upgrade pip
# --- Windows (PowerShell / Command Prompt) ---
# 1. Recommended: Use the `py -3` launcher to explicitly 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 a specific Python 3 minor release (e.g., Python 3.12 vs. 3.13):
- When multiple Python 3 versions are installed side by side, invoke the versioned binary or launcher 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 fast alternative):uv venv --python 3.12 .venv(can also automatically download Python 3.12 if not yet installed).
- macOS / Linux:
- When multiple Python 3 versions are installed side by side, invoke the versioned binary or launcher directly so the virtual environment binds to that exact interpreter:
- Fixing
ensurepip is not availableon Debian / Ubuntu:- 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 matchingpython3-venvpackage 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 skip activation and invoke the environment’s Python executable directly (.venv\Scripts\python.exe -m pip install requests).
- If PowerShell blocks activation with
- Avoid deprecated
pyvenvand legacyvirtualenv:pyvenvwas deprecated in Python 3.6 and removed in Python 3.8. Built-invenvships in every Python 3.3+ standard library with zero external dependencies.
- Never commit
.venvto Git or move/rename the directory after creation:- Add
.venv/to.gitignore(echo ".venv/" >> .gitignore). Because virtual environments embed absolute interpreter paths inpyvenv.cfgand activation scripts, moving or renaming the project folder breaks the environment—if moved, delete.venv(rm -rf .venv) and recreate it fromrequirements.txtorpyproject.toml.
- Add
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 (downloads missing Python 3 versions automatically) | High-speed modern workflows (10x–100x faster environment 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 dependencies (CUDA, GDAL, MKL). |
You must log in or register to comment.
