pytest-dev/pytest · critical · ImportError
{path}
Error message
{path} What it means
import_path raises ImportError(path) when the given file path does not exist on disk. pytest uses import_path to load test modules and conftest.py files, so a non-existent path usually means a stale reference or a deleted/moved file.
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 modView on GitHub (pinned to 98b357f69e)
Solutions
- Verify the file exists at the given path before importing
- Check for typos, broken symlinks, or moved files
- Regenerate the path list / clear caches pointing at the stale path
Example fix
// before
mod = import_path('tests/old_test.py')
// after
from pathlib import Path
p = Path('tests/new_test.py')
assert p.exists()
mod = import_path(p) Defensive patterns
Strategy: validation
Validate before calling
from pathlib import Path
def import_if_exists(path):
p = Path(path)
if not p.exists():
raise FileNotFoundError(p)
from _pytest.pathlib import import_path
return import_path(p) Type guard
from pathlib import Path
def path_exists(path) -> bool:
return Path(path).exists() Prevention
- Check path.exists() before calling import_path
- Regenerate path lists after renaming or moving test files
When it happens
Trigger: import_path called with a path that fails path.exists(); e.g. a conftest or test module path recorded by the rewriter/cache that has since been deleted, or a manually-constructed wrong path.
Common situations: Deleted or moved test files still referenced by an IDE/runner; symlinks pointing nowhere; misconfigured rootdir/python path; stale .pyc without source.
Related errors
- could not create numbered dir with prefix {prefix} in {root}
- cannot create lockfile in {p}
- Can't find module {module_name} at location {path}
- {module_name}, {module_file}, {path}
- Directory '{rootdir}' not found. Check your '--rootdir' opti
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/0d6a143f1156f599.json.
Report an issue: GitHub.