pytest-dev/pytest · error · ImportError

{path}

Error message

{path}

What it means

Raised by import_path (line 590) when the given path does not exist on disk (path.exists() is False). This is the first guard before any import-mode logic; a non-existent .py file cannot be imported, so ImportError(path) is raised. Used during collection when pytest resolves a test file path.

Solutions

  1. Verify the file exists: pathlib.Path(path).exists() before running.
  2. Restore the deleted/moved file or fix the path argument.
  3. Remove stale symlinks or broken references from the collection root.

Example fix

// before
pytest tests/test_missing.py
// after
pytest tests/test_existing.py
Defensive patterns

Strategy: validation

Validate before calling

import pathlib
if not pathlib.Path(path).exists():
    raise FileNotFoundError(path)

Type guard

def _path_exists(p) -> bool:
    import pathlib
    return pathlib.Path(p).exists()

Prevention

When it happens

Trigger: Collecting a test file that was deleted or moved after the session started; symbolic link pointing to a missing target; typo in a conftest or test path passed to pytest; py.path vs pathlib.Path mismatch yielding an empty path.

Common situations: git operations during a run that remove files; editor saves that truncate to empty then rewrite; broken symlinks in the test tree; --pyargs pointing at a non-installable module.

Related errors


AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11). Data as JSON: /api/errors/0d6a143f1156f599. Report an issue: GitHub.

Appendix: source

Thrown at src/_pytest/pathlib.py:590

          allows having same-named test modules in different places.

    :param root:
        Used as an anchor when mode == ImportMode.importlib to obtain
        a unique name for the module being imported so it can safely be stored
        into ``sys.modules``.

    :param consider_namespace_packages:
        If True, consider namespace packages when resolving module names.

    :raises ImportPathMismatchError:
        If after importing the given `path` and the module `__file__`
        are different. Only raised in `prepend` and `append` modes.
    """
    path = Path(path)
    mode = ImportMode(mode)

    if not path.exists():
        raise ImportError(path)

    if mode is ImportMode.importlib:
        # Try to import this module using the standard import mechanisms, but
        # without touching sys.path.
        try:
            _, module_name = resolve_pkg_root_and_module_name(
                path, consider_namespace_packages=consider_namespace_packages
            )
        except CouldNotResolvePathError:
            pass
        else:
            # If the given module name is already in sys.modules, do not import it again.
            with contextlib.suppress(KeyError):
                return sys.modules[module_name]

            mod = _import_module_using_spec(module_name, path, insert_modules=False)
            if mod is not None:
                return mod

View on GitHub (pinned to 0d6fbdeffa)