How do you create a virtual environment specifically using Python 3? | A1Lab@MZeeshanM to PythonEnglish • 3 days ago

How do you create a virtual environment specifically using Python 3?

message-square
0

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).
  • Debian / Ubuntu ensurepip is not available error:
    • 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 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
      
  • 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 bypass activation altogether by running the environment’s executables directly (.venv\Scripts\python.exe -m pip install requests).
  • Always use python -m venv, never legacy virtualenv or pyvenv:
    • pyvenv was deprecated in Python 3.6 and removed in Python 3.8. Built-in venv ships with every Python 3.3+ standard library without third-party dependencies.
  • Keep .venv out of Git (echo ".venv/" >> .gitignore) and never move/rename the folder:
    • Virtual environments hardcode absolute interpreter paths in pyvenv.cfg and bin/activate scripts; if you rename or move your project directory, delete .venv (rm -rf .venv) and recreate it cleanly 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 (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).