Metadata-Version: 2.5
Name: pywho
Version: 0.4.0
Summary: Debugging painkiller for Python developers - One command to explain your Python environment, trace imports, and detect shadows
Project-URL: Homepage, https://github.com/AhsanSheraz/pywho
Project-URL: Repository, https://github.com/AhsanSheraz/pywho
Project-URL: Issues, https://github.com/AhsanSheraz/pywho/issues
Project-URL: Documentation, https://pywho.readthedocs.io/
Project-URL: Changelog, https://github.com/AhsanSheraz/pywho/blob/main/CHANGELOG.md
Author-email: Ahsan Sheraz <ahsansheraz@gmail.com>
License-Expression: MIT
License-File: LICENSE
Keywords: cli,debug,devtools,diagnostic,environment,import,interpreter,python,trace,venv,virtualenv
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Software Development :: Debuggers
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: System :: Systems Administration
Classifier: Typing :: Typed
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: mypy>=2.4.0; extra == 'dev'
Requires-Dist: pytest-cov>=7.1.0; extra == 'dev'
Requires-Dist: pytest>=9.1.1; extra == 'dev'
Requires-Dist: ruff>=0.17.1; extra == 'dev'
Description-Content-Type: text/markdown

<p align="center">
  <b>pywho</b><br>
  <i>One command to explain your Python environment, trace imports, and detect shadows.</i>
</p>

<p align="center">
  <a href="https://github.com/AhsanSheraz/pywho/actions/workflows/ci.yml"><img src="https://github.com/AhsanSheraz/pywho/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
  <a href="https://pypi.org/project/pywho/"><img src="https://img.shields.io/pypi/v/pywho.svg" alt="PyPI version"></a>
  <a href="https://pypi.org/project/pywho/"><img src="https://img.shields.io/pypi/pyversions/pywho.svg" alt="Python versions"></a>
  <a href="https://github.com/AhsanSheraz/pywho/blob/main/LICENSE"><img src="https://img.shields.io/pypi/l/pywho.svg" alt="License"></a>
  <a href="https://pypi.org/project/pywho/"><img src="https://img.shields.io/pypi/dm/pywho.svg" alt="Downloads"></a>
  <a href="https://codecov.io/gh/AhsanSheraz/pywho"><img src="https://codecov.io/gh/AhsanSheraz/pywho/branch/main/graph/badge.svg" alt="Coverage"></a>
  <a href="https://pywho.readthedocs.io/"><img src="https://readthedocs.org/projects/pywho/badge/?version=latest" alt="Documentation"></a>
</p>

---

Ever asked *"Which Python am I running? Why did `import X` load that file? Do I have any files shadowing real modules? Why does this work on your machine and not on mine?"* — **pywho** answers all of it instantly.

📖 **[Read the full documentation](https://pywho.readthedocs.io/)**

## Quick Reference

| Command | Description |
|---------|-------------|
| `pywho` | Show a full report of your Python environment |
| `pywho --json` | Output environment report as JSON |
| `pywho --packages` | Include all installed packages in the report |
| `pywho --no-pip` | Skip pip version detection |
| `pywho trace <module>` | Trace where an import resolves and show search order |
| `pywho trace <module> --verbose` | Trace with full sys.path search log |
| `pywho trace <module> --json` | Trace output as JSON |
| `pywho scan .` | Scan project for files that shadow stdlib/installed packages |
| `pywho scan . --no-installed` | Scan against stdlib only (skip installed packages) |
| `pywho scan . --json` | Scan output as JSON |
| `pywho doctor` | Check the environment for the usual setup problems |
| `pywho diff a.json b.json` | Compare two environment snapshots |
| `pywho --python <path> ...` | Run any command inside another interpreter or virtual environment |
| `python -m pywho` | Run as a Python module |

## Table of Contents

- [Getting Started](#getting-started)
- [Why pywho?](#why-pywho)
- [Usage](#usage)
  - [Environment Inspection](#environment-inspection)
  - [Import Tracing](#import-tracing)
  - [Shadow Scanning](#shadow-scanning)
  - [Health Checks](#health-checks)
  - [Comparing Environments](#comparing-environments)
  - [Inspecting Another Interpreter](#inspecting-another-interpreter)
- [Python API](#python-api)
- [What It Detects](#what-it-detects)
- [Use Cases](#use-cases)
- [Platforms](#platforms)
- [Development](#development)
- [License](#license)

## Getting Started

```bash
pip install pywho
```

Or with [uv](https://docs.astral.sh/uv/):

```bash
uv add --dev pywho
```

For one-off usage without installing anything into the project:

```bash
uvx pywho --python .venv
```

> **Note:** `uvx pywho`, `pipx run pywho` and `uv run --with pywho pywho` all run pywho inside a separate, temporary environment, and on their own they describe that environment, not your project. Use `uvx` or `pipx run` together with `--python`. See [Inspecting Another Interpreter](#inspecting-another-interpreter).

## Why pywho?

- **"Works on my machine"** is the #1 debugging friction in Python. pywho kills it with one command.
- **Paste the output** into a GitHub issue, Slack message, or Stack Overflow question. Done.
- **Zero dependencies.** Pure stdlib. Works everywhere Python runs.
- **Cross-platform.** Linux, macOS, Windows. Python 3.10+.

## Usage

### Environment Inspection

```bash
pywho
```

```
  pywho - Python Environment Inspector
  ==============================================

  Interpreter
    Executable: /Users/dev/myproject/.venv/bin/python3
    Version: 3.12.3 (CPython)
    Compiler: Clang 15.0.0 (clang-1500.3.9.4)
    Architecture: 64-bit
    Build: main, Apr  9 2024, 08:09:14

  Platform
    System: Darwin 24.1.0
    Machine: arm64

  Virtual Environment
    Active: Yes
    Type: uv
    Path: /Users/dev/myproject/.venv
    Prompt: myproject

  Paths
    Prefix: /Users/dev/myproject/.venv
    Base Prefix: /opt/homebrew/Cellar/python@3.12/...
    Site-packages: /Users/dev/myproject/.venv/lib/python3.12/site-packages

  Package Manager
    Detected: uv
    pip version: 24.0

  sys.path
    [0] /Users/dev/myproject/.venv/bin
    [1] /opt/homebrew/.../python312.zip
    [2] /opt/homebrew/.../python3.12
    ...
```

#### JSON output (for CI, scripts, sharing)

```bash
pywho --json
```

#### Include installed packages

```bash
pywho --packages
```

#### Skip pip version check

```bash
pywho --no-pip
```

#### Run as module

```bash
python -m pywho
```

### Import Tracing

Trace exactly where an import resolves and why:

```bash
pywho trace requests
```

```
  Import Resolution: requests
  ===========================

    Resolved to: /Users/dev/myproject/.venv/lib/python3.12/site-packages/requests/__init__.py
    Module type: third-party (package)
    Package:     requests 2.32.3 (installed by uv; required by httpie)

    Search order:
      [0] /Users/dev/myproject -> not found
      [2] /opt/homebrew/.../python3.12 -> not found
      [4] /Users/dev/myproject/.venv/lib/python3.12/site-packages
           -> FOUND /Users/dev/myproject/.venv/lib/python3.12/site-packages/requests/__init__.py
```

What the report tells you:

- **Resolved to** is the file a fresh program started in this folder would import. The modules pywho itself has loaded do not decide the answer.
- **Package** names the installed distribution that put the file there, even when the names differ (`import yaml` comes from `PyYAML`), the tool that installed it, and the installed packages that require it.
- **Search order** numbers are the real positions in `sys.path`. The current folder is searched first, as it is when you run a script from it.

A dotted name such as `pywho trace requests.adapters` imports the parent package (`requests`) first, exactly as Python would. If that import fails, the error is shown instead of a crash.

#### Full search log

```bash
pywho trace json --verbose
```

#### JSON trace output

```bash
pywho trace requests --json
```

#### Shadow detection (single module)

```bash
$ pywho trace requests

  WARNING: '/Users/dev/myproject/requests.py' shadows installed package at '/Users/dev/myproject/.venv/lib/python3.12/site-packages/requests/__init__.py'
```

### Shadow Scanning

Scan an entire project for files that shadow stdlib or installed packages:

```bash
$ pywho scan .

  Found 2 shadow(s)
    2 HIGH (stdlib)

  [HIGH] json.py
         shadows stdlib module 'json'
  [HIGH] math.py
         shadows stdlib module 'math'
```

#### Scan with JSON output

```bash
pywho scan . --json
```

#### Scan stdlib only (skip installed packages)

```bash
pywho scan . --no-installed
```

Smart exclusions — these directories are automatically skipped: `.venv`, `__pycache__`, `node_modules`, `dist`, `build`, `.git`, and more.

"Installed" means importable from a `site-packages` folder. A project file never counts as shadowing itself, so the result is the same whether you run `pywho scan .`, `python -m pywho scan .`, or have the project installed in editable mode.

### Health Checks

`pywho doctor` looks for the usual reasons an environment misbehaves and tells you which one applies:

```bash
$ pywho doctor

  pywho doctor
  ============

  [ OK ] The activated virtual environment is in use.
  [FAIL] pip and python on PATH belong to different environments.
         python -> /Users/dev/myproject/.venv/bin/python (Python 3.12)
         pip    -> /opt/homebrew/bin/pip (Python 3.11)
         pip installs into -> /opt/homebrew/lib/python3.11/site-packages
  [WARN] Environment variables change where every Python looks for modules.
         PYTHONPATH=/Users/dev/old-libs
  [ OK ] Every sys.path entry exists.
  [ OK ] No duplicate or broken packages.
  [ OK ] No .pth file points to a missing folder.

  1 problem, 1 warning
```

| Check | What it catches |
|-------|-----------------|
| Activated environment | `VIRTUAL_ENV` points to one environment while another Python is running |
| `python` and `pip` on PATH | `pip install` puts packages where `python` never looks |
| Environment variables | `PYTHONPATH` or `PYTHONHOME` silently changing every Python |
| `sys.path` | Entries that do not exist |
| Packages | The same package installed in two versions, or a folder left without metadata by a failed uninstall |
| `.pth` files | Files pointing to a folder that is gone (a moved editable install) |
| Externally managed | A system Python that refuses `pip install` (PEP 668) |

Exit code is `1` when a check fails, `0` otherwise. Warnings are advice and do not change the exit code. Use `pywho doctor --json` in scripts.

### Comparing Environments

Save a snapshot on each machine, then let pywho show what differs:

```bash
# On each machine
pywho --json --packages > env.json

# Anywhere
pywho diff mine.json theirs.json
```

```
  pywho diff
  ==========
    A: mine.json
    B: theirs.json

  Environment
    interpreter.version: 3.12.3 -> 3.11.9
    platform.system: Darwin -> Linux

  Packages
    Only in A (1)
      idna 3.7
    Different version (1)
      requests 2.31.0 -> 2.32.3
```

Folder paths are left out of the comparison: they differ on every machine and would hide what matters. Exit code is `1` when the snapshots differ, `0` when they match. Snapshots made without `--packages` are compared without their package lists.

### Inspecting Another Interpreter

Every command accepts `--python`. pywho then runs inside that interpreter, which does **not** need pywho installed:

```bash
pywho --python .venv                     # a virtual environment folder
pywho --python /usr/bin/python3.11       # an interpreter
pywho --python python3 doctor            # a command on PATH
pywho --python ../other-project/.venv trace requests
```

This is what makes a single global install useful for every project:

```bash
uvx pywho --python .venv
pipx run pywho --python .venv doctor
```

The target must be Python 3.10 or newer. Nothing from pywho's own environment leaks into the report.

## Python API

### Environment inspection

```python
from pywho import inspect_environment

report = inspect_environment()

print(report.executable)        # /usr/bin/python3
print(report.venv.is_active)    # True
print(report.venv.type)         # "uv"
print(report.package_manager)   # "uv"

# Get JSON-serializable dict
data = report.to_dict()
```

### Import tracing

```python
from pywho import trace_import

trace = trace_import("requests")
print(trace.resolved_path)    # /path/to/requests/__init__.py
print(trace.module_type)      # ModuleType.THIRD_PARTY
print(trace.provided_by)      # [ProvidingPackage(name='requests', version='2.32.3', ...)]
print(trace.shadows)          # [] (empty = no shadows)

if trace.shadows:
    for s in trace.shadows:
        print(f"WARNING: {s.description}")
```

### Shadow scanning

```python
from pywho import scan_path
from pathlib import Path

results = scan_path(Path("."))
for r in results:
    print(f"[{r.severity.value.upper()}] {r.path} shadows {r.module_name}")
```

### Health checks

```python
from pywho import run_doctor

for check in run_doctor():
    print(check.status.value, check.summary)
```

### Comparing environments

```python
from pywho import diff_reports, inspect_environment
from pywho.differ import load_snapshot

diff = diff_reports(inspect_environment().to_dict(), load_snapshot("theirs.json"))
for package in diff.packages:
    print(package.name, package.a, "->", package.b)
```

## What It Detects

### Interpreters
- CPython, PyPy, and other implementations
- Version, compiler, architecture (32/64-bit)
- Build date

### Virtual environments

| Type | Detection method |
|------|-----------------|
| **venv** | `sys.prefix != sys.base_prefix` |
| **virtualenv** | `virtualenv =` in `pyvenv.cfg` |
| **uv** | `uv =` in `pyvenv.cfg` |
| **conda** | `conda-meta` folder in `sys.prefix` |
| **poetry** | `POETRY_ACTIVE` env var, inside a virtual environment |
| **pipenv** | `PIPENV_ACTIVE` env var, inside a virtual environment |

A virtual environment created on top of conda is reported as the virtual environment it is, not as conda.

### Package managers
Detects: pip, uv, conda, poetry, pipenv, pyenv

### Paths
- `sys.prefix`, `sys.base_prefix`, `sys.exec_prefix`
- All `site-packages` directories
- Full `sys.path` with index numbers

### Packages
With `--packages`: lists all installed packages with versions and locations.

### Import shadows
- Single-module detection via `pywho trace`
- Project-wide scanning via `pywho scan`
- Severity levels: **HIGH** (stdlib) and **MEDIUM** (installed)

### Environment problems
- Activated environment that is not the running Python
- `pip` and `python` from different environments
- `PYTHONPATH` / `PYTHONHOME` overrides
- Packages installed in two versions and broken package folders
- Stale `.pth` files and missing `sys.path` entries

## Use Cases

| Scenario | Command |
|----------|---------|
| Debug "works on my machine" | `pywho --json --packages > env.json`, then `pywho diff mine.json theirs.json` |
| Verify onboarding setup | `pywho doctor` |
| CI environment snapshots | `pywho --json --packages > env.json` |
| Fail CI on a broken environment | `pywho doctor` (exit code 1 on a problem) |
| Find why an import is wrong | `pywho trace <module>` |
| Find which package provides a module | `pywho trace <module>` |
| Audit project for shadows | `pywho scan .` |
| Inspect a project from a global install | `pywho --python .venv` |
| Ask users for their env | "Please run `pywho`" |

## Platforms

| Platform | Status |
|----------|--------|
| Linux | ✅ Supported |
| macOS | ✅ Supported |
| Windows | ✅ Supported |

| Python | Status |
|--------|--------|
| 3.10+ | ✅ Supported |
| 3.10 – 3.15 | ✅ Tested in CI |

## Development

```bash
git clone https://github.com/AhsanSheraz/pywho.git
cd pywho
uv venv && source .venv/bin/activate
uv pip install -e ".[dev]"
pytest -v
mypy src/pywho
```

## License

[MIT](LICENSE)
