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 libraryvenvmodule creates an isolated environment tied to whichever interpreter runs-m venv, meaningpython3.12(or the Windowspylauncher target) must already exist on your system. - Listing available Python versions before creating the environment:
- Windows: Run
py --list(orpy -0p) to view all installed Python versions and their executable paths. - Linux / macOS: Run
which -a python3 python3.11 python3.12 python3.13to locate installed binaries, or pass a full path directly (/usr/local/bin/python3.11 -m venv .venv).
- Windows: Run
- Automatically downloading and binding any Python version with
uv: If the desired Python version is not installed locally, Astral’suvtool can download a standalone Python build and create a standard.venvin one command:uv venv --python 3.12 .venv - Using third-party
virtualenvwith-p/--python: If you use the third-partyvirtualenvpackage (pip install virtualenv), pass-pfollowed 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
.venvwith 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.venvfolder, create a fresh.venvwith the new interpreter, and runpip 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. |
You must log in or register to comment.
