Skip to main content

importguard

A pull-request report of what a change adds to your Python program's startup, and why.

pip install importguardpy
importguard diff

One added import, three modules deep, can pull hundreds of modules into your CLI's or service's startup. It can also add a network call or a subprocess that now runs on every launch. None of that shows up in the diff a reviewer reads. importguard runs the import before and after the change and reports, on the pull request:

  • every new package at startup, with the chain of imports that pulled it in and the line in your code to change;
  • forbidden modules (forbid = ["pandas"]), even when they arrive through a dependency;
  • side effects at import time: network, subprocess, file writes, environment changes, threads.

There's nothing to set up: no config, no lockfile, no baseline. Problems that already exist on your main branch aren't reported, so the first run is quiet.


What a pull request sees

The GitHub Action keeps one comment on the PR up to date:

❌ importguard diff

Compared with adb7d13f6c (merge-base of HEAD and main).

entrypoint status modules time
demoapp.cli fail 103 (base 77) 32ms

⚠️ demoapp.cli: new third-party package at startup: bigdep (+20 modules, ~9ms); also pulls in stdlib: sqlite3, json, csv, http, socket, …

src/demoapp/cli.py:2 -> demoapp.report
src/demoapp/report.py:5 -> bigdep
  • defer the import at src/demoapp/report.py:5 - mark it lazy (Python 3.15+), list it in __lazy_modules__, or move it into the function that uses it lazy import bigdep
  • if this is intended, a maintainer approves it outside this change: label the pull request importguard:allow:bigdep …

❌ demoapp.cli: network access during import in demoapp.telemetry (src/demoapp/telemetry.py:3): socket.getaddrinfo 127.0.0.1:9

src/demoapp/cli.py:8 -> demoapp.telemetry
  • move this out of module scope in demoapp.telemetry (e.g. into a function called explicitly or on first use)

By default it reports and does not gate. A new startup package is a ⚠️ warning on the PR, so the reviewer answers one question: "this adds bigdep to CLI startup through report.py:5. Intended?" Only rules you set fail the check:

  • forbid lists;
  • network or subprocess calls at import time;
  • new_packages = "error", if you want new dependencies at startup to need explicit approval.

In practice it stays quiet. Replayed over the last 50 commits of httpie, black, mkdocs, pre-commit and cookiecutter (250 commits), it raised no false alarms, and runs took under a second each (median). Startup regressions are rare, so the report is meant to cost you almost nothing until the day one lands.

Set it up on GitHub

# .github/workflows/importguard.yml
on:
  pull_request:
    # labeled/unlabeled re-run the report when a maintainer adds an approval label
    types: [opened, synchronize, reopened, labeled, unlabeled]
permissions:
  contents: read
  pull-requests: write   # to keep the PR comment up to date
jobs:
  importguard:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with: { fetch-depth: 0 }            # history for the merge-base
      - uses: actions/setup-python@v5
        with: { python-version: "3.13" }
      - run: pip install -e .               # your project, with its dependencies
      - uses: AryanKumar1401/importguard@v0.2.0

The action:

  • compares the PR with its merge-base;
  • writes the report to the job summary;
  • keeps one PR comment updated (comment: false to turn that off);
  • fails the job only on errors (args: --strict to fail on warnings too).

If it can't comment, it warns and carries on.

Pull requests from forks

On a PR from a fork, GitHub gives the workflow a read-only token, so the action can't comment there. Instead it uploads the report, and a second workflow posts it:

# .github/workflows/importguard-comment.yml
name: importguard comment
on:
  workflow_run:
    workflows: [importguard]          # the name of the workflow that runs the action
    types: [completed]
permissions:
  actions: read                       # to download the report
  pull-requests: write                # to comment
jobs:
  comment:
    if: github.event.workflow_run.event == 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - uses: AryanKumar1401/importguard/comment@v0.2.0

workflow_run always runs this file from your default branch, with a write token. It downloads the report the PR's run uploaded and never checks out or runs the PR's code. The report comes from a run the fork controls, so it is handled as untrusted data:

  • the comment goes only to the PR whose head is the commit that run tested;
  • the body is size-capped;
  • @mentions are defused.

Don't use pull_request_target to get a write token instead. importguard executes the code it checks, so that would hand the token to whoever opened the PR.

Locally, or in any other CI, importguard diff does the same against origin/HEAD, main or master, or --base REF. --format markdown prints the comment and --json prints a versioned report.

Who decides what is acceptable

When agents write much of the code, a check is only useful if the change it judges can't adjust the check. diff keeps everything that decides the verdict outside the change:

  • The baseline is the merge-base commit, checked out into a temporary git worktree and probed with the same interpreter. Nothing is stored in the repository, so there is nothing to regenerate.
  • The policy comes from the base too. That covers forbid, allow, severities, and the python, pythonpath and timeout used to run the probe. If the base has no [tool.importguard], the defaults apply, never the change's own config. A change that edits the policy gets a policy_changed finding, and the edit takes effect after it's reviewed and merged.
  • Approval happens outside the diff. It comes from a maintainer's PR label (importguard:allow:<package>, or importguard:approved for everything), --allow / IMPORTGUARD_ALLOW in CI, or allow on the base branch. A suggested fix never includes a command an agent could run to approve itself.
  • Entrypoints are the union of base and head, so deleting one from the config doesn't hide it.
  • An entrypoint the base cannot import (new in the change) is compared with what the base's other entrypoints load. If the base fails only because the change removed a third-party dependency, that module is stubbed out on the base and the report says so.

Labels are only as trustworthy as whoever can set them. If agents run with a token that can label PRs, put approvals in allow on the protected base branch instead.

What it does not protect against: the probe runs inside the interpreter it measures. Code written specifically to deceive it, such as rewriting the probe's state or faking its output, can succeed. Such code stands out in review. importguard guards against accidental regressions and casual workarounds; it is not a sandbox.

Configuration

Everything is optional. Without configuration, entrypoints are inferred from [project.scripts] and your package name.

# pyproject.toml (diff reads this from the base commit)
[tool.importguard]
entrypoints = ["mypkg", "mypkg.cli"]
forbid = ["pandas", "torch"]                    # never at startup, even transitively
allow = ["rich"]                                # approved third-party packages
forbid_effects = ["network", "subprocess"]      # default; file_write, env and thread warn
new_packages = "warning"                        # default; "error" to require approval
new_stdlib = "info"
new_first_party = "info"
max_ms = 300                                    # advisory: a warning, never a failure...
strict_time = false                             # ...unless you opt in
# python = ".venv/bin/python"                   # interpreter to probe with
# pythonpath = ["src"]                          # default: src/ if it exists

[tool.importguard.entrypoint."mypkg.cli"]       # per-entrypoint overrides
forbid = ["boto3"]                              # added to the global list

Why module sets and not milliseconds: wall-clock import time on shared CI runners varies by tens of percent between identical runs, so a millisecond threshold either flakes or is too loose to catch anything. The set of modules an entrypoint loads is deterministic. importguard reports time as context and judges by what loads.

Commands

command what it does
importguard diff [ENTRY...] The report. What the working tree adds to startup vs. its merge-base (--base REF, --allow PKG, --strict)
importguard why ENTRY MODULE The import chain from ENTRY to MODULE, with file:line for each hop
importguard show ENTRY Modules an entrypoint loads, by cumulative cost (--kind third_party, --all)
importguard effects [ENTRY...] Side effects performed during import
importguard snapshot / check Lockfile mode (below)
importguard schema JSON Schema of --json output

Common options: --json / --format {text,json,markdown}, --python PATH, --config PATH, -q.

Exit codes: 0 ok · 1 errors (or warnings with --strict) · 2 usage/config error · 3 tool error.

Lockfile mode

diff compares two commits in one environment, so it can't see a dependency upgrade that starts importing something new. To cover that, record what each entrypoint loads and compare against it:

$ importguard snapshot     # writes importguard.lock; commit it
$ importguard check        # compares with the lock; check --changed re-probes only affected entrypoints

The lockfile lives in the repository, so a change can rewrite it. Protect it with CODEOWNERS and keep diff as the PR report.

For coding agents

  • importguard diff --json gives a versioned report (importguard schema). Each finding has:
    • a stable id;
    • the import chain with file:line for each hop;
    • typed remedies (defer_import with the exact replacement, request_approval, investigate).
  • It's deterministic, needs no setup, and judges the change from outside it. An agent gets to green by fixing the import, not by editing the config.

See docs/agents.md for an AGENTS.md snippet and a Claude Code Stop hook that runs importguard diff before an agent finishes its turn.

How it works

importguard launches your interpreter with -X importtime and a small stdlib-only probe script. Before importing the entrypoint, the probe installs:

  • a sys.meta_path observer, which records each module the first time it is imported, together with the frame (file, line, importing module) that asked for it;
  • a sys.addaudithook hook, which records network, subprocess, file-write, environment and thread events and attributes each to the module whose top-level code was running.

It also:

  • flags project code that runs at interpreter startup (sitecustomize, .pth files);
  • confirms which modules really loaded, so deleting one from sys.modules doesn't hide it;
  • treats git-ignored or vendored code inside the project as third-party;
  • compares namespace packages by distribution (google.cloud.bigquery, not google);
  • ignores bytecode-cache writes and Cython's bookkeeping modules.

The probe executes your code. Treat untrusted repositories as you would when running their tests: in a sandbox.

How it fits with other tools

tool relationship
Ruff (TID251/TID253, lazy-import rules), flake8-lazy Static checks of your own source; use them alongside. importguard sees imports that arrive through dependencies, and runtime side effects.
importcost Measures import cost, and its audit/apply make imports lazy (PEP 810) by running your tests. To migrate to lazy import, use importcost; importguard reports what each PR adds.
tuna, importtime-waterfall Visualise one -X importtime run. importguard show and why answer the same questions in CI or from an agent.
import-linter Architecture contracts between your own packages, checked statically.

Development

git clone https://github.com/AryanKumar1401/importguard && cd importguard
pip install -e ".[dev]"
pytest
ruff check src tests && mypy

License

MIT

Metadata

Release files for importguardpy 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for importguardpy 0.2.0
File Size Uploaded Provenance Actions
importguardpy-0.2.0.tar.gz 54.8 kB With provenance Details for importguardpy-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for importguardpy 0.2.0
File Interpreter ABI Platform Actions
importguardpy-0.2.0-py3-none-any.whl Python 3 none any Details for importguardpy-0.2.0-py3-none-any.whl

Total release size: 98.9 kB

Release files / importguardpy-0.2.0.tar.gz

Download URL importguardpy-0.2.0.tar.gz
Size 54.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7f3ec8bb6b7990e3165a200e61f3805171becbcd5e3f0639ee9f6bde37ac0628
BLAKE2b-256 checksum
How to use checksums
596f994408c98383cc20b797298b981089164624dd150d1b16c1b33b71caf0b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

Release files / importguardpy-0.2.0-py3-none-any.whl

Download URL importguardpy-0.2.0-py3-none-any.whl
Size 44.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b20c6f4d4a4ec5adf42a79a9853f2fa515910ca1113c56dbfe603f5f0f451e8a
BLAKE2b-256 checksum
How to use checksums
377fbde4e9f38d018c620090d58a1aa984b6a16318fca2b31ce69f263ddc8d50
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

Security for importguardpy 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for importguardpy-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page