{"id":"0bbe69afd994aa06","repo":"pytest-dev/pytest","slug":"module-name-module-file-path","errorCode":null,"errorMessage":"{module_name}, {module_file}, {path}","messagePattern":"\\{module_name\\}, \\{module_file\\}, \\{path\\}","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/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/pathlib.py#L632-L668","documentation":"In prepend/append import mode, after importing the module pytest compares its __file__ against the requested path. If module.__file__ is None (the module has no on-disk file, e.g. a built-in, frozen, or namespace module), it raises ImportPathMismatchError to avoid silently binding the wrong module.","triggerScenarios":"import_path in prepend/append mode resolves module_name to a module whose __file__ is None, then tries the mismatch check at pathlib.py:648-650 and raises. PY_IGNORE_IMPORTMISMATCH is not set.","commonSituations":"A test/conftest file whose name collides with a built-in or frozen module; namespace package resolving to a module without a file; exotic import hooks.","solutions":["Set PY_IGNORE_IMPORTMISMATCH=1 to skip the mismatch check","Rename the file so its module name does not collide with a built-in/frozen module","Fix the package layout so the module resolves to a real .py file"],"exampleFix":"// before\n# file named enum.py colliding with stdlib enum\n// after\nmv tests/enum.py tests/test_enum.py","handlingStrategy":"fallback","validationCode":"import os\ndef should_skip_mismatch_check():\n    return os.environ.get('PY_IGNORE_IMPORTMISMATCH', '') == '1'","typeGuard":null,"tryCatchPattern":"try:\n    mod = import_path(path, mode='prepend')\nexcept ImportPathMismatchError:\n    os.environ['PY_IGNORE_IMPORTMISMATCH'] = '1'\n    mod = import_path(path, mode='prepend')","preventionTips":["Avoid naming test files after built-in or frozen modules","Set PY_IGNORE_IMPORTMISMATCH=1 only when the layout intentionally yields a None __file__"],"tags":["pathlib","import","import-mismatch","package","pytest"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}