Python Interpreter Discovery

1.2.1 · active · verified Wed Apr 01

python-discovery is a Python library designed for robust discovery of Python interpreters installed on a machine. It can locate Python versions from various sources like system packages, pyenv, mise, asdf, uv, and the Windows registry (PEP 514). It verifies candidates and returns detailed metadata, with results cached for faster repeated lookups. The library is actively maintained, with frequent minor releases addressing bug fixes and feature enhancements. The current version is 1.2.1.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `get_interpreter` to find Python installations based on version specifiers, leveraging `DiskCache` for performance. It attempts to find any Python 3 interpreter and then a specific range of Python 3 versions.

from pathlib import Path
from python_discovery import DiskCache, get_interpreter
import os

# Define a cache root path; for a quickstart, we use a demo-specific path.
# In a real application, consider a persistent user cache (e.g., ~/.cache/python-discovery).
cache_root = Path.home() / ".cache" / "python-discovery-quickstart-demo"
cache_root.mkdir(parents=True, exist_ok=True)
cache = DiskCache(root=cache_root)

# Discover any Python 3 interpreter available on the system
print("Attempting to discover a Python 3.x interpreter...")
python_3_interpreter = get_interpreter("python3", cache=cache)

if python_3_interpreter:
    print(f"Found Python 3 interpreter: {python_3_interpreter.executable}")
    print(f"Version: {python_3_interpreter.version}")
    print(f"Prefix: {python_3_interpreter.prefix}")
else:
    print("Could not find a Python 3 interpreter.")

# Discover a specific Python version using PEP 440 specifier
print("\nAttempting to discover Python >=3.9,<3.12...")
specific_interpreter = get_interpreter(">=3.9,<3.12", cache=cache)

if specific_interpreter:
    print(f"Found Python matching spec: {specific_interpreter.executable}")
    print(f"Version: {specific_interpreter.version}")
else:
    print("Could not find a Python interpreter matching '>=3.9,<3.12'.")

view raw JSON →