{"id":"7e115955b59f4f17","repo":"pytest-dev/pytest","slug":"can-t-find-module-module-name-at-location-path","errorCode":null,"errorMessage":"Can't find module {module_name} at location {path}","messagePattern":"Can't find module (.+?) at location (.+?)","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"critical","filePath":"src/_pytest/pathlib.py","lineNumber":618,"sourceCode":"            pass\n        else:\n            # If the given module name is already in sys.modules, do not import it again.\n            with contextlib.suppress(KeyError):\n                return sys.modules[module_name]\n\n            mod = _import_module_using_spec(module_name, path, insert_modules=False)\n            if mod is not None:\n                return mod\n\n        # Could not import the module with the current sys.path, so we fall back\n        # to importing the file as a single module, not being a part of a package.\n        module_name = module_name_from_path(path, root)\n        with contextlib.suppress(KeyError):\n            return sys.modules[module_name]\n\n        mod = _import_module_using_spec(module_name, path, insert_modules=True)\n        if mod is None:\n            raise ImportError(f\"Can't find module {module_name} at location {path}\")\n        return mod\n\n    try:\n        pkg_root, module_name = resolve_pkg_root_and_module_name(\n            path, consider_namespace_packages=consider_namespace_packages\n        )\n    except CouldNotResolvePathError:\n        pkg_root, module_name = path.parent, path.stem\n\n    # Change sys.path permanently: restoring it at the end of this function would cause surprising\n    # problems because of delayed imports: for example, a conftest.py file imported by this function\n    # might have local imports, which would fail at runtime if we restored sys.path.\n    if mode is ImportMode.append:\n        if str(pkg_root) not in sys.path:\n            sys.path.append(str(pkg_root))\n    elif mode is ImportMode.prepend:\n        if str(pkg_root) != sys.path[0]:\n            sys.path.insert(0, str(pkg_root))","sourceCodeStart":600,"sourceCodeEnd":636,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/pathlib.py#L600-L636","documentation":"In import_mode=importlib, pytest first tries spec-based and module-name-based import; if both fail to load the module at the given path it raises ImportError('Can't find module {module_name} at location {path}'). Means the file exists but could not be imported as a module under the current sys.path/package layout.","triggerScenarios":"Running pytest with --import-mode=importlib against a file whose derived module_name cannot be imported: missing parent package, broken __init__.py, sys.path not including the package root, or a module-name collision.","commonSituations":"Misconfigured package structure, missing __init__.py in a namespace, test files outside any package, stale sys.path entries, name shadowing.","solutions":["Add/fix the missing __init__.py so the file is part of an importable package","Ensure the package root is on sys.path (check rootdir / pythonpath config)","Rename a file that shadows a stdlib or installed module name","If layout is unusual, consider switching to --import-mode=prepend"],"exampleFix":"// before\n# tests/helpers/util.py with no __init__.py and import-mode=importlib\n// after\n# add empty tests/__init__.py and tests/helpers/__init__.py","handlingStrategy":"validation","validationCode":"def importable_in_importlib_mode(path, root):\n    from pathlib import Path\n    p = Path(path)\n    if not p.exists():\n        return False\n    # require that the file is inside a package with __init__.py chain or a top-level module\n    return p.is_file() and ('__init__.py' in [c.name for c in p.parent.iterdir()] or p.parent == Path(root))","typeGuard":null,"tryCatchPattern":"try:\n    mod = import_path(path, mode='importlib')\nexcept ImportError as e:\n    if 'Can\\'t find module' in str(e):\n        # fall back to prepend mode or fix layout\n        mod = import_path(path, mode='prepend')","preventionTips":["Keep a consistent package layout with __init__.py files when using importlib mode","Ensure the package root is on sys.path or configured via pythonpath/pytest.ini"],"tags":["pathlib","import","import-mode","package","pytest"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}