{"record":{"id":"ffc102ba7994420d","repo":"cocoindex-io/cocoindex","slug":"could-not-create-loader-for-file-app-path","errorCode":null,"errorMessage":"Could not create loader for file: {app_path}","messagePattern":"Could not create loader for file: (.+?)","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"python/cocoindex/user_app_loader.py","lineNumber":82,"sourceCode":"        # `if __name__ == \"__main__\":` block during module loading.\n        module_name = os.path.splitext(os.path.basename(app_path))[0]\n\n        # If the file lives inside a package (directory has __init__.py),\n        # load it as a proper submodule so that relative imports work.\n        if os.path.isfile(os.path.join(app_dir, \"__init__.py\")):\n            root_parent, package_parts = _find_package_root(app_dir)\n            return _import_as_package_module(root_parent, package_parts, module_name)\n\n        if app_dir not in sys.path:\n            sys.path.insert(0, app_dir)\n        try:\n            spec = importlib.util.spec_from_file_location(module_name, app_path)\n            if spec is None:\n                raise ImportError(f\"Could not create spec for file: {app_path}\")\n            module = importlib.util.module_from_spec(spec)\n            sys.modules[spec.name] = module\n            if spec.loader is None:\n                raise ImportError(f\"Could not create loader for file: {app_path}\")\n            spec.loader.exec_module(module)\n            return module\n        except (ImportError, FileNotFoundError, PermissionError) as e:\n            raise Error(f\"Failed importing file '{app_path}': {e}\") from e\n        finally:\n            if app_dir in sys.path and sys.path[0] == app_dir:\n                sys.path.pop(0)\n\n    # If the target looks like a bare module name (e.g. \"main\") and a\n    # corresponding file exists in the CWD inside a package, load via the\n    # package-qualified name so relative imports work.\n    candidate_file = os.path.join(os.getcwd(), app_target + \".py\")\n    cwd = os.getcwd()\n    if os.path.isfile(candidate_file) and os.path.isfile(\n        os.path.join(cwd, \"__init__.py\")\n    ):\n        root_parent, package_parts = _find_package_root(cwd)\n        return _import_as_package_module(root_parent, package_parts, app_target)","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/cocoindex-io/cocoindex/blob/e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b/python/cocoindex/user_app_loader.py#L64-L100","documentation":"Raised (as ImportError, then wrapped as `Failed importing file ...`) when a spec was created for the app file but its `spec.loader` is None, meaning Python has a spec yet no loader capable of executing the file. Like the spec failure, it indicates the file is not a loadable Python module.","triggerScenarios":"Loading an app file whose file type maps to a spec without an executable loader (e.g. unusual extensions, namespace/namespace-package-like entries, or files Python recognizes but cannot exec via the source-file loader).","commonSituations":"Very rare in practice; seen with non-.py source files (e.g. `.pyc` handled differently), custom filesystem setups, or tooling that intercepts import machinery.","solutions":["Use a standard `.py` source file as the app target.","Check the wrapped `Failed importing file '...': ...` message for the underlying cause.","If you intentionally use exotic loaders, load the module yourself and pass a dotted module name to cocoindex instead."],"exampleFix":"// before\ncocoindex update ./app/some.pyd   # not a Python source file\n// after\ncocoindex update ./app/main.py","handlingStrategy":"validation","validationCode":"spec = importlib.util.spec_from_file_location(\"m\", app_path)\nassert spec is not None and spec.loader is not None, f\"no loader for {app_path}\"","typeGuard":null,"tryCatchPattern":"try:\n    app = cocoindex.user_app_loader.load_user_app(app_target)\nexcept cocoindex.Error as e:\n    if 'Could not create loader' in str(e):\n        print(f'{app_target} has no importable loader; use a .py file'); sys.exit(1)\n    raise","preventionTips":["Stick to plain `.py` source files as app targets.","Test your app loads with `python -c \"import importlib.util,sys; ...\"`-style import before wiring into cocoindex.","For exotic module formats, import the module yourself and pass its dotted name to the CLI."],"tags":["python","import","cli","app-loading"],"backgroundTag":"module-init-failed","analyzedSha":"e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b","analyzedAt":"2026-09-08T15:59:19.997Z","contentChangedAt":"2026-09-08T15:59:19.997Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}