{"record":{"id":"63557d91b8f84bfe","repo":"pytest-dev/pytest","slug":"module-name-module-file-path","errorCode":null,"errorMessage":"({module_name}, {module_file}, {path})","messagePattern":"\\((.+?), (.+?), (.+?)\\)","errorType":"exception","errorClass":"ImportPathMismatchError","httpStatus":null,"severity":"error","filePath":"src/_pytest/pathlib.py","lineNumber":650,"sourceCode":"        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))\n    else:\n        assert_never(mode)\n\n    importlib.import_module(module_name)\n\n    mod = sys.modules[module_name]\n    if path.name == \"__init__.py\":\n        return mod\n\n    ignore = os.environ.get(\"PY_IGNORE_IMPORTMISMATCH\", \"\")\n    if ignore != \"1\":\n        module_file = mod.__file__\n        if module_file is None:\n            raise ImportPathMismatchError(module_name, module_file, path)\n\n        if module_file.endswith((\".pyc\", \".pyo\")):\n            module_file = module_file[:-1]\n        if module_file.endswith(os.sep + \"__init__.py\"):\n            module_file = module_file[: -(len(os.sep + \"__init__.py\"))]\n\n        try:\n            is_same = _is_same(str(path), module_file)\n        except FileNotFoundError:\n            is_same = False\n\n        if not is_same:\n            raise ImportPathMismatchError(module_name, module_file, path)\n\n    return mod\n\n\ndef _import_module_using_spec(","sourceCodeStart":632,"sourceCodeEnd":668,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/pathlib.py#L632-L668","documentation":"Raised by import_path (prepend/append modes) when the imported module's __file__ is None (lines 648-650). After importlib.import_module succeeds, the helper checks mod.__file__; a None file (typical of namespace packages or built-in modules) cannot be matched against the collected path, so ImportPathMismatchError is raised to avoid silently testing the wrong code.","triggerScenarios":"Collecting a file that resolves to a namespace package (no __init__.py) whose __file__ is None; a module shadowed by a built-in or C extension; __init__.py-less package layouts combined with prepend import mode.","commonSituations":"Namespace packages in prepend/append mode; mixing importlib and prepend modes; a local file name colliding with a stdlib module.","solutions":["Switch to importlib import mode: --import-mode=importlib.","Add __init__.py to make the package a regular package with a real __file__.","Rename the colliding module to avoid shadowing a namespace/builtin."],"exampleFix":"// before\npytest --import-mode=prepend\n// after\npytest --import-mode=importlib","handlingStrategy":"fallback","validationCode":"import importlib\nmod = importlib.import_module(module_name)\nif getattr(mod, '__file__', None) is None:\n    # will mismatch; choose importlib mode or add __init__.py","typeGuard":"def _module_has_file(modname) -> bool:\n    import importlib\n    return getattr(importlib.import_module(modname), '__file__', None) is not None","tryCatchPattern":null,"preventionTips":["Prefer --import-mode=importlib for namespace/PEP 420 packages.","Add __init__.py to make packages regular."],"tags":["pytest","pathlib","import-mismatch","collection"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}