Skip to content

IECQ004: external_dependencies

Third-party imports are declared in pyproject.toml.

Category: code_quality ยท Applies to: eval, helper

What it does

Collects every import in the package and treats one as external when it is not in the standard library, not in [project].dependencies, not local to the package and not the repository's own package. For an evaluation, each external import must be declared in some [project.optional-dependencies] group or [dependency-groups] entry (other than dev), or in the isolated package's pyproject.toml when isolated-packages-dir is set, and the evaluation must own a group named after itself unless it is isolated.

For a helper package the rule is different, because every evaluation that imports the helper loads whatever it imports at module level: those imports must be in [project].dependencies, while imports inside a function, a try block or an if TYPE_CHECKING: block are deferred and only need to be declared in some group or in any isolated package. Helpers need no group of their own. One diagnostic per import, at its first site.

Import-to-distribution mapping uses the packages installed in the current environment plus a few static aliases, so run the linter inside the project's environment. Names are compared in PEP 503 normalised form.

Why is this bad?

An import nobody declared works on the author's machine and fails for the next person with ModuleNotFoundError, often only when a particular sample runs.

Example

[project.optional-dependencies]
my_eval = ["datasets>=4.0", "scikit-learn"]

Options

  • isolated-packages-dir
  • import-prefix

Suppress on a line with # inspect-evals-lint: ignore[IECQ004] or ignore[external_dependencies]; select or ignore it in configuration by either, or by the prefix IECQ.