{"record":{"id":"64fc032550ceaa56","repo":"jax-ml/jax","slug":"cannot-determine-the-name-of-the-caller","errorCode":null,"errorMessage":"Cannot determine the ``__name__`` of the caller.","messagePattern":"Cannot determine the ``__name__`` of the caller\\.","errorType":"error_code","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"jax/_src/lazy_loader.py","lineNumber":40,"sourceCode":"\ndef attach(package_name: str, submodules: Sequence[str]) -> tuple[\n    Callable[[str], Any],\n    Callable[[], list[str]],\n    list[str],\n]:\n  \"\"\"Lazily loads submodules of a package.\n\n  Returns:\n    A tuple of ``__getattr__``, ``__dir__`` function and ``__all__`` --\n    a list of available global names, which can be used to replace the\n    corresponding definitions in the package.\n\n  Raises:\n    RuntimeError: If the ``__name__`` of the caller cannot be determined.\n  \"\"\"\n  owner_name = sys._getframe(1).f_globals.get(\"__name__\")\n  if owner_name is None:\n    raise RuntimeError(\"Cannot determine the ``__name__`` of the caller.\")\n\n  __all__ = list(submodules)\n\n  def __getattr__(name: str) -> Any:\n    if name in submodules:\n      value = importlib.import_module(f\"{package_name}.{name}\")\n      # Update module-level globals to avoid calling ``__getattr__`` again\n      # for this ``name``.\n      assert owner_name is not None  # pyrefly#40\n      setattr(sys.modules[owner_name], name, value)\n      return value\n    raise AttributeError(f\"module '{package_name}' has no attribute '{name}'\")\n\n  def __dir__() -> list[str]:\n    return __all__\n\n  return __getattr__, __dir__, __all__\n","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lazy_loader.py#L22-L58","documentation":"jax._src.lazy_loader.attach builds a PEP 562 lazy module by inspecting the caller frame's globals for __name__. If sys._getframe(1).f_globals has no __name__ key (e.g. exec'd namespace or unusual import machinery), it raises this RuntimeError because it cannot know which module to patch with lazily imported attributes.","triggerScenarios":"Calling lazy_loader.attach from code executed via exec() with a bare dict globals, from a REPL-like namespace without __name__, or from an embedding context where the frame globals lack __name__.","commonSituations":"Almost never seen by end users; occurs in exotic embedding, notebook kernels, or custom import hooks that strip __name__ from globals, or when attach is invoked inside a function called through C extensions where frame introspection is unusual.","solutions":["Call attach at module top level in a real .py module so the caller frame has __name__","Pass a namespace that includes __name__ if you must exec the code","Avoid wrapping the attach call inside exec/eval or lambda frames","Update JAX — this is internal API misuse, not a user configuration issue"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import sys\nframe_globals = sys._getframe(1).f_globals\nassert '__name__' in frame_globals, 'caller must be a real module'","typeGuard":null,"tryCatchPattern":"try:\n    lazy_loader.attach(...)\nexcept RuntimeError:\n    # execute inside a real module file instead\n    ...","preventionTips":["Call lazy_loader.attach only at module top level","Avoid exec()-based module loading around JAX internals"],"tags":["jax","lazy-loading","import-machinery","internal-api"],"backgroundTag":"module-import-introspection-failure","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}