{"record":{"id":"14186d830e5e50b0","repo":"pypa/pip","slug":"not-a-package","errorCode":null,"errorMessage":"Not a package:","messagePattern":"Not a package:","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/pkg_resources/__init__.py","lineNumber":2462,"sourceCode":"    try:\n        spec = importer.find_spec(packageName)\n    except AttributeError:\n        # capture warnings due to #1111\n        with warnings.catch_warnings():\n            warnings.simplefilter(\"ignore\")\n            loader = importer.find_module(packageName)\n    else:\n        loader = spec.loader if spec else None\n\n    if loader is None:\n        return None\n    module = sys.modules.get(packageName)\n    if module is None:\n        module = sys.modules[packageName] = types.ModuleType(packageName)\n        module.__path__ = []\n        _set_parent_ns(packageName)\n    elif not hasattr(module, '__path__'):\n        raise TypeError(\"Not a package:\", packageName)\n    handler = _find_adapter(_namespace_handlers, importer)\n    subpath = handler(importer, path_item, packageName, module)\n    if subpath is not None:\n        path = module.__path__\n        path.append(subpath)\n        importlib.import_module(packageName)\n        _rebuild_mod_path(path, packageName, module)\n    return subpath\n\n\ndef _rebuild_mod_path(orig_path, package_name, module: types.ModuleType):\n    \"\"\"\n    Rebuild module.__path__ ensuring that all entries are ordered\n    corresponding to their sys.path order\n    \"\"\"\n    sys_path = [_normalize_cached(p) for p in sys.path]\n\n    def safe_sys_path_index(entry):","sourceCodeStart":2444,"sourceCodeEnd":2480,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/pkg_resources/__init__.py#L2444-L2480","documentation":"Raised as TypeError inside _handle_ns when an existing sys.modules entry for packageName lacks __path__. Namespace packages require __path__ (a package marker); a plain module only has __file__. The code at line 2461 checks `not hasattr(module, '__path__')` and raises, meaning the name was already imported as a non-package module.","triggerScenarios":"Calling fixup_namespace_packages() or declaring a namespace where a module with the same name was already imported as a regular module (not a package). This happens in _handle_ns at line 2462 when iterating sys.path items for namespace package registration.","commonSituations":"A name conflict where both a module file (e.g. foo.py) and a namespace package declaration exist for the same name; importing a single-file module before declaring it as a namespace package; mixing implicit PEP 420 namespace packages with legacy declare_namespace calls.","solutions":["Ensure the name being declared as a namespace package is not also defined as a single-file module (.py) on sys.path.","If the module was imported prematurely, restructure so the namespace declaration happens before any sub-imports.","Migrate from pkg_resources.declare_namespace to PEP 420 implicit namespace packages (remove __init__.py from namespace dirs).","Search sys.path for a conflicting module file with the same name and remove or rename it."],"exampleFix":"// before: foo.py exists AND declare_namespace('foo') is called\nimport foo  # imports foo.py as a module\nfrom pkg_resources import declare_namespace\ndeclare_namespace('foo')  # TypeError: Not a package\n\n// after: remove foo.py, make foo/ a directory without __init__.py (PEP 420)\n// or ensure foo/ has __init__.py with declare_namespace before any import of foo","handlingStrategy":"validation","validationCode":"import sys\ndef is_namespace_safe(name: str) -> bool:\n    mod = sys.modules.get(name)\n    if mod is not None and not hasattr(mod, '__path__'):\n        return False  # exists as a module, not a package\n    return True","typeGuard":"import types\ndef is_package(name: str) -> bool:\n    mod = sys.modules.get(name)\n    return mod is not None and isinstance(getattr(mod, '__path__', None), list)\n","tryCatchPattern":"from pkg_resources import declare_namespace\ntry:\n    declare_namespace('foo')\nexcept TypeError:\n    # name conflicts with an existing module; restructure packages\n    pass\n","preventionTips":["Avoid name collisions between modules and namespace packages.","Migrate to PEP 420 implicit namespace packages instead of declare_namespace.","Check sys.modules for conflicts before declaring a namespace."],"tags":["namespace-packages","pkg-resources","import","module-conflict"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}