jax-ml/jax · error · RuntimeError

Cannot determine the ``__name__`` of the caller.

Error message

Cannot determine the ``__name__`` of the caller.

What it means

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.

Solutions

  1. Call attach at module top level in a real .py module so the caller frame has __name__
  2. Pass a namespace that includes __name__ if you must exec the code
  3. Avoid wrapping the attach call inside exec/eval or lambda frames
  4. Update JAX — this is internal API misuse, not a user configuration issue
Defensive patterns

Strategy: validation

Validate before calling

import sys
frame_globals = sys._getframe(1).f_globals
assert '__name__' in frame_globals, 'caller must be a real module'

Try / catch

try:
    lazy_loader.attach(...)
except RuntimeError:
    # execute inside a real module file instead
    ...

Prevention

When it happens

Trigger: 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__.

Common situations: 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.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/64fc032550ceaa56. Report an issue: GitHub.

Appendix: source

Thrown at jax/_src/lazy_loader.py:40

def attach(package_name: str, submodules: Sequence[str]) -> tuple[
    Callable[[str], Any],
    Callable[[], list[str]],
    list[str],
]:
  """Lazily loads submodules of a package.

  Returns:
    A tuple of ``__getattr__``, ``__dir__`` function and ``__all__`` --
    a list of available global names, which can be used to replace the
    corresponding definitions in the package.

  Raises:
    RuntimeError: If the ``__name__`` of the caller cannot be determined.
  """
  owner_name = sys._getframe(1).f_globals.get("__name__")
  if owner_name is None:
    raise RuntimeError("Cannot determine the ``__name__`` of the caller.")

  __all__ = list(submodules)

  def __getattr__(name: str) -> Any:
    if name in submodules:
      value = importlib.import_module(f"{package_name}.{name}")
      # Update module-level globals to avoid calling ``__getattr__`` again
      # for this ``name``.
      assert owner_name is not None  # pyrefly#40
      setattr(sys.modules[owner_name], name, value)
      return value
    raise AttributeError(f"module '{package_name}' has no attribute '{name}'")

  def __dir__() -> list[str]:
    return __all__

  return __getattr__, __dir__, __all__

View on GitHub (pinned to 1e1c6a8fc0)