{"record":{"id":"f88739777f73315f","repo":"pytest-dev/pytest","slug":"can-t-find-module-modname-at-location-self-s","errorCode":null,"errorMessage":"Can't find module {modname} at location {self!s}","messagePattern":"Can't find module (.+?) at location (.+?)","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"src/_pytest/_py/path.py","lineNumber":1099,"sourceCode":"        if ensuresyspath is False no modification of syspath happens.\n\n        Special value of ensuresyspath==\"importlib\" is intended\n        purely for using in pytest, it is capable only of importing\n        separate .py files outside packages, e.g. for test suite\n        without any __init__.py file. It effectively allows having\n        same-named test modules in different places and offers\n        mild opt-in via this option. Note that it works only in\n        recent versions of python.\n        \"\"\"\n        if not self.check():\n            raise error.ENOENT(self)\n\n        if ensuresyspath == \"importlib\":\n            if modname is None:\n                modname = self.purebasename\n            spec = importlib.util.spec_from_file_location(modname, str(self))\n            if spec is None or spec.loader is None:\n                raise ImportError(f\"Can't find module {modname} at location {self!s}\")\n            mod = importlib.util.module_from_spec(spec)\n            spec.loader.exec_module(mod)\n            return mod\n\n        pkgpath = None\n        if modname is None:\n            pkgpath = self.pypkgpath()\n            if pkgpath is not None:\n                pkgroot = pkgpath.dirpath()\n                names = self.new(ext=\"\").relto(pkgroot).split(self.sep)\n                if names[-1] == \"__init__\":\n                    names.pop()\n                modname = \".\".join(names)\n            else:\n                pkgroot = self.dirpath()\n                modname = self.purebasename\n\n            self._ensuresyspath(ensuresyspath, pkgroot)","sourceCodeStart":1081,"sourceCodeEnd":1117,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/_py/path.py#L1081-L1117","documentation":"Raised by LocalPath.pyimport() in importlib mode when importlib.util.spec_from_file_location returns a spec without a loader (or None). This means Python's import machinery could not determine how to load a module from the given file path — typically because the file extension is not a recognized importable format (.py, .so, etc.). The error is an ImportError, distinct from the ENOENT raised earlier when the file does not exist.","triggerScenarios":"Calling path.pyimport(ensuresyspath='importlib') on a .txt, .json, or extension-less file; on a path whose suffix is not registered with any import loader; with an explicit modname that importlib still cannot resolve to a loader.","commonSituations":"Dynamically importing test data or generated modules with unexpected extensions; cross-platform issues with .pyc vs .py; plugin systems that point at non-Python files.","solutions":["Ensure the target file has a Python-recognized extension (.py, .pyc, .so).","Register a custom loader/path hook if importing a non-standard extension.","Verify the path exists and is readable before calling pyimport.","If importing source, use importlib.util directly with explicit source handling."],"exampleFix":"// before\nmod = path.pyimport()  # path points to 'data.txt'\n// after\nmod = path_with_py_extension.pyimport()","handlingStrategy":"validation","validationCode":"import importlib.util\nIMPORTABLE_EXTS = {'.py', '.pyc', '.pyo', '.so'}\ndef can_import(path) -> bool:\n    if not path.check(file=True):\n        return False\n    if path.ext not in IMPORTABLE_EXTS:\n        return False\n    spec = importlib.util.spec_from_file_location(path.purebasename, str(path))\n    return spec is not None and spec.loader is not None","typeGuard":"def has_importable_ext(path) -> bool:\n    return path.ext in {'.py', '.pyc', '.so'}","tryCatchPattern":"try:\n    mod = path.pyimport()\nexcept ImportError as e:\n    if \"Can't find module\" in str(e):\n        # fallback: load source manually or skip\n        ...","preventionTips":["Validate the file extension before pyimport.","Use importlib.util directly for finer control over non-standard extensions."],"tags":["py-path","localpath","import","importlib"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}