{"record":{"id":"3883c75ace57bee5","repo":"apache/beam","slug":"module-parts-0-not-found-in-sys-modules","errorCode":null,"errorMessage":"Module {parts[0]} not found in sys.modules","messagePattern":"Module (.+?) not found in sys\\.modules","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/internal/code_object_pickler.py","lineNumber":439,"sourceCode":"\ndef get_code_from_identifier(code_object_identifier: str):\n  \"\"\"Returns the code object corresponding to the code object identifier.\n\n  Args:\n    code_object_identifier: A string representing the code object identifier.\n\n  Returns:\n    The code object.\n\n  Raises:\n    ValueError: If the path is empty or invalid.\n    AttributeError: If the attribute is not found.\n  \"\"\"\n  if not code_object_identifier:\n    raise ValueError('Path must not be empty.')\n  parts = code_object_identifier.split('.')\n  if parts[0] not in sys.modules:\n    raise AttributeError(f'Module {parts[0]} not found in sys.modules')\n  obj = sys.modules[parts[0]]\n  for part in parts[1:]:\n    if name_result := _SINGLE_NAME_PATTERN.fullmatch(part):\n      obj = _get_code_object_from_single_name_pattern(\n          obj, name_result, code_object_identifier)\n    elif lambda_with_args_result := _LAMBDA_WITH_ARGS_PATTERN.fullmatch(part):\n      obj = _get_code_object_from_lambda_with_args_pattern(\n          obj, lambda_with_args_result, code_object_identifier)\n    elif lambda_with_hash_result := _LAMBDA_WITH_HASH_PATTERN.fullmatch(part):\n      obj = _get_code_object_from_lambda_with_hash_pattern(\n          obj, lambda_with_hash_result, code_object_identifier)\n    elif default_result := _DEFAULT_PATTERN.fullmatch(part):\n      index = int(default_result.group(2))\n      if index >= len(obj.__defaults__):\n        raise ValueError(\n            f'Index {index} is out of bounds for obj.__defaults__'\n            f' {len(obj.__defaults__)} in path {code_object_identifier}')\n      obj = getattr(obj, '__defaults__')[index]","sourceCodeStart":421,"sourceCodeEnd":457,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/internal/code_object_pickler.py#L421-L457","documentation":"get_code_from_identifier requires the first dot-separated component of the path to be a module currently present in sys.modules. If the top-level module name is not importable/loaded, it raises AttributeError 'Module {parts[0]} not found in sys.modules'. This ensures identifiers only resolve against modules the current process has actually imported.","triggerScenarios":"Calling get_code_from_identifier('notimported.func') where 'notimported' was never imported; module imported under a different top-level name (aliasing or package re-rooting); identifier created in __main__ context being resolved where the module has a real package name.","commonSituations":"Identifiers serialized from a notebook/script (__main__) and replayed in a worker where the module is installed under a different name; missing dependency on the worker; typos in the module part of the path.","solutions":["Import the module explicitly before resolving: `import mymodule` then call get_code_from_identifier.","Ensure the module name in the identifier matches sys.modules (print sys.modules keys to verify).","Avoid pickling functions defined in __main__; move them into an importable installed package with consistent naming.","Install the required package on the worker so the module can be imported."],"exampleFix":"// before\nget_code_from_identifier('helpers.transform')  # helpers never imported\n// after\nimport helpers\nget_code_from_identifier('helpers.transform')","handlingStrategy":"type-guard","validationCode":"import sys\ndef module_part_loaded(path):\n    root = path.split('.', 1)[0]\n    return root in sys.modules","typeGuard":"def is_importable_module_path(path) -> bool:\n    import sys\n    return isinstance(path, str) and path.split('.', 1)[0] in sys.modules","tryCatchPattern":"try:\n    code = get_code_from_identifier(path)\nexcept AttributeError as e:\n    import importlib\n    mod = path.split('.', 1)[0]\n    try:\n        importlib.import_module(mod)\n        code = get_code_from_identifier(path)\n    except ImportError:\n        raise RuntimeError(f'Module {mod} missing on this environment') from e","preventionTips":["Import the module before resolving identifiers.","Avoid defining serialized functions in __main__; use installable packages.","Ensure all dependencies are installed on workers (requirements.txt/container image)."],"tags":["python","apache-beam","pickling","import-error"],"backgroundTag":"module-init-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}