Skip to main content

importroute

Tests

Explain where a Python name would be found in an explicit, ordered set of directories, without importing the target package.

Quickstart

python -m pip install "git+https://github.com/anishpentyala/importroute.git"
mkdir -p demo/checkout demo/site
printf 'raise RuntimeError("do not import this")\n' > demo/checkout/widget.py
printf 'pass\n' > demo/site/widget.py
importroute widget --path demo/checkout --path demo/site --expect-under demo/site --json

This exits 1 with top-level FAILED and per-module EXPECTATION_FAILED: the first root selects the checkout's widget.py, outside the expected site directory. The exception in that file does not run. Reverse the two --path arguments and this example passes. This models declared filesystem roots, not custom import hooks or a live process's full import behavior.

A PyPI release is being prepared. Until its package page is linked here, use the source-install command above.

A checkout can hide an installed dependency. A regular package in a later root can replace earlier namespace portions. A package can also win over a same-name module at one root. importroute shows the candidates and the route through each dotted-name component, and can fail a CI check when that route leaves an expected directory.

This is a bounded filesystem model, not proof of what a running process imports. It does not evaluate initializers, custom finders, editable-install hooks, .pth files, archives, the module cache, or a package's dynamic __path__ changes. It does not prove that selected code can load or is safe.

Install

Python 3.10 or newer. No runtime dependencies.

python -m pip install .
importroute --help

Run the tool with the interpreter whose default extension suffixes you want to model. Installation from this checkout is the supported installation instruction. No package-registry release is assumed.

A reproducible shadowing check

mkdir -p demo/checkout demo/site
printf 'raise RuntimeError("do not import this")\n' > demo/checkout/widget.py
printf 'pass\n' > demo/site/widget.py

importroute widget --path demo/checkout --path demo/site

The report selects demo/checkout/widget.py and lists demo/site/widget.py as a later-root candidate. Paths are displayed as absolute paths. The first file's exception does not run.

Require the selected route to stay under the intended directory:

importroute widget --path demo/checkout --path demo/site \
  --expect-under demo/site --json

This exits 1 with top-level FAILED and per-module EXPECTATION_FAILED, because the first directory wins. Reversing the two --path arguments makes this example pass.

For a dotted name:

importroute acme.plugins.reader --path ./src --path ./vendor --json

Each component has its own search_roots, candidates, selection, ignored_roots, and errors. Child lookup follows the selected package directories, not the original top-level roots. Regular packages are assumed to retain their default directory __path__.

Python API

from importroute import trace_imports

report = trace_imports(
    ["widget", "acme.plugins.reader"],
    ["./src", "./vendor"],
    expected_root="./src",
)
print(report["status"])

Inputs must be iterables, not one string. Module names are absolute dotted identifiers. Relative names and path/glob patterns are rejected. Paths are literal: no environment-variable or tilde expansion. An empty path means the current directory. The function does not change sys.path.

Selection rules

The model uses exact-case directory entries and the running interpreter's default extension, source, then legacy-bytecode suffix order.

  • Within one root, a regular package beats a same-name module, including extension modules. Among matching files, default loader suffix order applies.
  • Across roots, the first concrete module or regular package wins.
  • Namespace portions combine in root order only if there is no concrete match. A later concrete match discards earlier namespace portions.
  • A plain module cannot have a child. A selected regular package's missing child does not fall back to unrelated top-level roots.
  • __pycache__ entries without source are not treated as legacy same-directory .pyc candidates. Extension and bytecode files are located, never loaded or validated.

--expect-under checks the physical path of every selected component, including every namespace portion. A symlink pointing outside fails even when its lexical path is inside. The expected root must be an existing directory. This is a location check, not a trust or containment sandbox.

Reports and exit codes

JSON reports use format: "importroute/v1". All queries are retained. The overall status gives INCOMPLETE priority over failures.

Exit Meaning
0 Every query is LOCATED, with any expectation satisfied
1 A query is MISSING, PARENT_NOT_PACKAGE, or EXPECTATION_FAILED
2 Invalid input, command usage, or output failure
3 At least one query is INCOMPLETE

An INCOMPLETE step may include a tentative selection from readable roots. Do not treat it as a passing result. Unreadable directories, non-directory roots such as archives, scan-budget exhaustion, and failed physical-path checks are explicit failures to complete the model. Missing roots are listed and ignored. Even an unsupported root after an apparent winner causes INCOMPLETE, because this tool promises a candidate inventory, not only a winner.

Top-level names with builtin or frozen precedence in this interpreter return INCOMPLETE instead of pretending a directory candidate is the runtime winner. Other non-filesystem behavior is excluded by the stated assumptions, not detected automatically.

Limits per call: 128 queries, 64 initial roots, 32 components per query, and 100,000 directory entries. One additional entry may be observed to detect that the entry budget was exceeded. Directory-name caches live only for the call. These are work limits, not a timeout: a slow filesystem or network mount can still block an operating-system call.

When to use something else

For the spec selected by the real import system, use importlib.util.find_spec. Looking up a dotted name there can execute its parent package. If runtime behavior matters, reproduce the real process and inspect the actual imported module instead of relying on this model.

Ruff's A005 checks standard-library name shadowing. deptry checks dependency declarations and imports. Import Linter checks architecture contracts. Those solve different problems; this tool does not replace them. Its narrower task is an explicit-directory route explanation with all candidates and an expected-root gate.

Detect Shadowed Modules is a closer alternative for duplicate-directory checks across an environment's search paths. Its CLI includes uv editable locations and installed-distribution ownership; its Python API also accepts explicit roots. Prefer it for that environment-wide inventory. importroute instead follows requested dotted names, includes ordinary module files and loader suffix precedence, distinguishes namespace accumulation from concrete-package selection, and checks the physical path of every selected component against an expected root.

Do not run Python itself on a hostile checkout merely because this tool does not import target packages. Interpreter startup, the installed tool, and its own imports remain part of your execution environment. Use a trusted installation and working directory. Filesystem observations can race with concurrent changes and are not an atomic snapshot. The model intentionally does not follow platform case-folding or PYTHONCASEOK behavior.

Development

python -m unittest discover -s tests -v

Tests compare 25 two-root layout pairs with the standard-library finder and 120 seeded nested layouts with actual harmless imports in isolated child processes. They also cover namespace precedence, suffix selection, symlinks, scan limits, no target execution, and output failures. This is Linux validation, not a claim of complete cross-platform import emulation.

See design and sources for the scope and evidence.

MIT licensed.

Metadata

Release files for importroute 0.1.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 importroute 0.1.0
File Size Uploaded Provenance Actions
importroute-0.1.0.tar.gz 20.4 kB Without provenance Details for importroute-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 31.5 kB

Release files / importroute-0.1.0.tar.gz

Download URL importroute-0.1.0.tar.gz
Size 20.4 kB
Tags Source
SHA-256 checksum
How to use checksums
93074a76ee53145e9183ceaafcc357997fa8a1d2dc7a54c692524a4d3a665b80
BLAKE2b-256 checksum
How to use checksums
73b34a5a4d2ce33c1f3f24f8c586e35974356fbd9428a763400ae88d4d1b2141
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.18

Release files / importroute-0.1.0-py3-none-any.whl

Download URL importroute-0.1.0-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ef4d7903718d1d802c3d4c86b2705f4e64f6d50d41ad733ba559d40e69008e9a
BLAKE2b-256 checksum
How to use checksums
5e31dbd507c3a0f6dc185f0ee522d47ef649fb0cdb51a98dcd9f0f3e6397d20f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.18

Security for importroute 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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