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 (`s | A1Lab@MZeeshanM to PythonEnglish • 3 days ago

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 (`s

message-square
0
1
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 (`s
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 (`s | A1Lab@MZeeshanM to PythonEnglish • 3 days ago

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).
  • Fixing ensurepip is not available on Debian / Ubuntu:
    • Minimal Debian and Ubuntu installations omit ensurepip from the base python3 package, causing python3 -m venv .venv to fail with The virtual environment was not created successfully because ensurepip is not available. Install the matching python3-venv package first:
      sudo apt update && sudo apt install -y python3-venv
      # Or for a specific version: sudo apt install -y python3.12-venv
      
  • Windows PowerShell ExecutionPolicy error on Activate.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).
  • Avoid deprecated pyvenv and legacy virtualenv:
    • pyvenv was deprecated in Python 3.6 and removed in Python 3.8. Built-in venv ships in every Python 3.3+ standard library with zero external dependencies.
  • Never commit .venv to Git or move/rename the directory after creation:
    • Add .venv/ to .gitignore (echo ".venv/" >> .gitignore). Because virtual environments embed absolute interpreter paths in pyvenv.cfg and activation scripts, moving or renaming the project folder breaks the environment—if moved, delete .venv (rm -rf .venv) and recreate it from requirements.txt or pyproject.toml.

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).