Skip to main content

sys.meta_path diagnostics: find out who is messing with Python's import machinery

Project description

metapathology

sys.meta_path diagnostics — a tiny, stdlib-only debug tool that tells you who is messing with Python's import machinery: which finder claimed each import, who mutated sys.meta_path (and when, with a stack trace), and which modules were loaded in ways that bypass sys.path_hooks.

Built for debugging import hook contention — the class of bug where two libraries (e.g. an editable-install finder and a sys.path_hooks-based instrumenter like beartype.claw) both want a say in how a module is loaded, and one silently loses. See beartype#556 for the motivating incident.

Status

First working version: all three layers, CLI, and exit report are implemented and tested. Published to PyPI.

Usage

Primary: run your program under observation, no code changes needed —

$ python -m metapathology myscript.py --my-args
$ python -m metapathology -m pytest tests/

A report is printed at exit. Prefer python -m metapathology over a bare metapathology command: it guarantees the hooks land in the same interpreter and venv as the code under investigation.

Library API, for when a wrapper isn't possible (notebooks, embedded interpreters, "I can only touch conftest.py"):

import metapathology

metapathology.install()  # as early as possible

No dependencies, no configuration required. It's a debug tool: run it, read the report, remove it.

How it works

Three layers, escalating in invasiveness, covering each other's blind spots:

  1. Audit hook (sys.addaudithook, passive, irremovable) — snapshots sys.meta_path on every import event; detects wholesale reassignment (sys.meta_path = [...]), which nothing else can catch.
  2. Instrumented sys.meta_path — a list subclass whose mutation methods log who appended/inserted/removed finders, with stack traces, at mutation time. Re-installed automatically if layer 1 sees it blown away.
  3. Finder attribution — each finder's find_spec is shadowed with a logging wrapper in its instance dict (so third-party isinstance scans of sys.meta_path still pass), recording exactly which finder claimed each module.

The exit report cross-references this log against sys.modules and a fresh PathFinder.find_spec() replay to flag modules that a meta-path finder short-circuited away from the standard sys.path_hooks chain.

Caveats

  • CPython only (relies on the import audit event and import-system internals).
  • A debug tool by design: it deliberately perturbs sys.meta_path in reversible ways. Don't ship it enabled in production.

Related search terms

sys.meta_path diagnostics, import hook conflict, PEP 302 compliance, meta path finder debugging, sys.path_hooks bypass, editable install import hook, who imported this module.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metapathology-0.1.0.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

metapathology-0.1.0-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file metapathology-0.1.0.tar.gz.

File metadata

  • Download URL: metapathology-0.1.0.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for metapathology-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d3fed33eb41bca56c409edf8c28a1d4e96dd4b589fbf40df7d8308ed6b95779d
MD5 25806fa9939f3e398ecd495ad8752954
BLAKE2b-256 83797cc6c965a2f537cdc25aecde26490e0ac4a006f58150d0b8238a678db7db

See more details on using hashes here.

File details

Details for the file metapathology-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: metapathology-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for metapathology-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6483a1d26f1614cdc92cc8cc6b0408f9429a2534643b92da2e5594592cefb4ca
MD5 5023e3c6a2987689f599e11c1ff35330
BLAKE2b-256 487d4d98f2f284217b5dad08c388fab275883e5a65c49d3b48a6c79b75597c4a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page