Skip to content

Locate a command on PATH

Return the expanded path of an executable, or not installed.

18th August 2026

import os
import shutil
from typing import Optional


def generate_expanded_path() -> str:
    current_path = os.environ.get('PATH', '')
    path_elements = current_path.split(os.pathsep)
    expanded = [os.path.expanduser(path) for path in path_elements]
    return os.pathsep.join(expanded)


def which(command: str) -> str:
    full_path: Optional[str] = shutil.which(command, path=generate_expanded_path())
    if full_path is None:
        return "not installed"
    return full_path