How do you create a virtualenv using a specific Python version? | A1Lab@MZeeshanM to PythonEnglish • 6 days ago

How do you create a virtualenv using a specific Python version?

message-square
0

Invoke the built-in venv module using the exact target Python binary—such as python3.12 -m venv .venv on Linux/macOS or py -3.12 -m venv .venv on Windows—or use uv venv --python 3.12 / virtualenv -p /path/to/python3.12 .venv if you want automatic version downloading or legacy virtualenv features.

Basic Example

# Linux / macOS: call the specific Python interpreter directly with -m venv
python3.12 -m venv .venv

# Windows (using the Python Launcher 'py'): specify the major.minor version flag
py -3.12 -m venv .venv

# Activate the virtual environment and verify the bound Python version
source .venv/bin/activate        # Linux / macOS
# .venv\Scripts\Activate.ps1     # Windows PowerShell

python --version                 # Output: Python 3.12.x

Key Details & Alternative Tools

  • The target Python version must already be installed (for venv): Python’s standard library venv module creates an isolated environment tied to whichever interpreter runs -m venv, meaning python3.12 (or the Windows py launcher target) must already exist on your system.
  • Listing available Python versions before creating the environment:
    • Windows: Run py --list (or py -0p) to view all installed Python versions and their executable paths.
    • Linux / macOS: Run which -a python3 python3.11 python3.12 python3.13 to locate installed binaries, or pass a full path directly (/usr/local/bin/python3.11 -m venv .venv).
  • Automatically downloading and binding any Python version with uv: If the desired Python version is not installed locally, Astral’s uv tool can download a standalone Python build and create a standard .venv in one command:
    uv venv --python 3.12 .venv
    
  • Using third-party virtualenv with -p / --python: If you use the third-party virtualenv package (pip install virtualenv), pass -p followed by either a version specifier or an explicit executable path:
    virtualenv -p python3.11 .venv
    virtualenv -p "C:\Python311\python.exe" .venv
    
  • Recreating an existing .venv with a new Python version: You cannot change the Python version of an existing virtual environment in place. Export your dependencies (pip freeze > requirements.txt), delete the old .venv folder, create a fresh .venv with the new interpreter, and run pip install -r requirements.txt.

Comparison of Version-Specific Virtual Environment Tools

Tool / Command OS Support Auto-Downloads Missing Python? Requires Extra Install? Best Used When
python3.12 -m venv .venv Linux / macOS No (must be pre-installed) No (built into Python 3.3+) Standard, zero-dependency environments on Unix/macOS.
py -3.12 -m venv .venv Windows No (must be pre-installed) No (includes Windows py launcher) Switching cleanly between multiple installed Python versions on Windows.
uv venv --python 3.12 Windows / Linux / macOS Yes Yes (pip install uv / binary) Fast environment creation when the target Python isn’t installed yet.
virtualenv -p 3.12 .venv Windows / Linux / macOS No (searches local paths) Yes (pip install virtualenv) Legacy workflows needing virtualenv seeding or custom discovery plugins.