pytest-dev/pytest · error · CouldNotResolvePathError
Could not resolve for
Error message
Could not resolve for {path} What it means
Raised by resolve_pkg_root_and_module_name (lines 925-932) when no package root can be found for the given path. The function walks start and its parents, computing a module name and checking importability; if none of the candidates yields an importable module name, CouldNotResolvePathError (a subclass of ImportError) is raised. This is used by importlib import mode to map a file path to a canonical module name.
Solutions
- Ensure the file's package root is on sys.path (add a conftest.py or __init__.py).
- Run pytest from the project root so the default rootdir insertion applies.
- Enable namespace package consideration if applicable.
- Add the directory via PYTHONPATH or a conftest at a higher level.
Example fix
// before (file outside any importable package) pytest tests/test_x.py # tests has no __init__.py and is not on sys.path // after touch tests/__init__.py # or add a conftest.py at project root
Defensive patterns
Strategy: validation
Validate before calling
try:
resolve_pkg_root_and_module_name(path)
except CouldNotResolvePathError:
# add parent to sys.path or add __init__.py Type guard
def _resolvable(path) -> bool:
try:
resolve_pkg_root_and_module_name(path)
return True
except CouldNotResolvePathError:
return False Try / catch
try:
pkg_root, name = resolve_pkg_root_and_module_name(path)
except CouldNotResolvePathError:
# fall back to path.parent / path.stem or fix sys.path
pkg_root, name = path.parent, path.stem Prevention
- Ensure the package root is on sys.path (add conftest.py / __init__.py).
- Run pytest from the project root so default sys.path insertion applies.
When it happens
Trigger: A test file outside any importable package (no parent with __init__.py on sys.path); a path in a directory not on sys.path; namespace package resolution disabled and no regular package root found; consider_namespace_packages=False with a namespace layout.
Common situations: Running pytest from a directory not on sys.path; flat test files without package structure; broken sys.path in a custom runner; namespace packages when consideration is off.
Related errors
- Can't find module at location
- {path}
- ( , , )
- cannot create lockfile in
- cannot delete key in keywords dict
AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11).
Data as JSON: /api/errors/b66815e510f88e4e.
Report an issue: GitHub.
Appendix: source
Thrown at src/_pytest/pathlib.py:932
pkg_root: Path | None = None
pkg_path = resolve_package_path(path)
if pkg_path is not None:
pkg_root = pkg_path.parent
if consider_namespace_packages:
start = pkg_root if pkg_root is not None else path.parent
for candidate in (start, *start.parents):
module_name = compute_module_name(candidate, path)
if module_name and is_importable(module_name, path):
# Point the pkg_root to the root of the namespace package.
pkg_root = candidate
break
if pkg_root is not None:
module_name = compute_module_name(pkg_root, path)
if module_name:
return pkg_root, module_name
raise CouldNotResolvePathError(f"Could not resolve for {path}")
def is_importable(module_name: str, module_path: Path) -> bool:
"""
Return if the given module path could be imported normally by Python, akin to the user
entering the REPL and importing the corresponding module name directly, and corresponds
to the module_path specified.
:param module_name:
Full module name that we want to check if is importable.
For example, "app.models".
:param module_path:
Full path to the python module/package we want to check if is importable.
For example, "/projects/src/app/models.py".
"""
try:
# Note this is different from what we do in ``_import_module_using_spec``, where we explicitly search throughView on GitHub (pinned to 0d6fbdeffa)