sgl-project/sglang · error · ImportError

could not import any module prefix of '{qualified_name}'

Error message

could not import any module prefix of '{qualified_name}'

What it means

Raised by source_patcher's _resolve_target when it cannot importlib.import_module any dotted prefix of the qualified name. The resolver walks candidate split points (to handle module attrs like pkg.mod.Class.method) and only raises if every prefix import fails. This means the module itself is missing — not the attribute (a missing attr would raise AttributeError instead).

Source

Thrown at python/sglang/srt/debug_utils/source_patcher/code_patcher.py:183

def _resolve_target(qualified_name: str) -> Callable[..., Any]:
    """Resolve 'pkg.mod.Class.method' to the actual function object.

    Tries progressively shorter module paths from right to left,
    then uses getattr for the remaining attribute chain.
    """
    parts: list[str] = qualified_name.split(".")

    target: Any = None
    for split_idx in range(len(parts), 0, -1):
        module_path: str = ".".join(parts[:split_idx])
        try:
            target = importlib.import_module(module_path)
            attr_parts: list[str] = parts[split_idx:]
            break
        except ImportError:
            continue
    else:
        raise ImportError(f"could not import any module prefix of '{qualified_name}'")

    for attr_name in attr_parts:
        target = getattr(target, attr_name)

    if isinstance(target, classmethod):
        target = target.__func__
    if not callable(target):
        raise TypeError(
            f"resolved target '{qualified_name}' is not callable: {type(target)}"
        )

    return target

View on GitHub (pinned to 0132848349)

Solutions

  1. Verify the module path: run python -c "import pkg.module" and fix the path in the patch spec
  2. If the import fails due to an error inside the module (not ModuleNotFoundError), fix that underlying import error first
  3. Regenerate or update patch YAML files after upgrading sglang so targets match the new module layout
  4. If the symbol moved, grep for it in the new version and update the qualified name

Example fix

# before
spec = PatchSpec(target='sglang.srt.layers.old_path.fn', ...)

# after
spec = PatchSpec(target='sglang.srt.layers.new_path.fn', ...)
Defensive patterns

Strategy: validation

Validate before calling

import importlib
parts = qualified_name.split('.')
if not any(_try_import('.'.join(parts[:i])) for i in range(1, len(parts) + 1)):
    raise ConfigError(f'no importable module prefix for {qualified_name}')

def _try_import(mod):
    try:
        importlib.import_module(mod); return True
    except ImportError:
        return False

Type guard

def resolves_to_module_prefix(qualified_name: str) -> bool:
    import importlib
    parts = qualified_name.split('.')
    return any(
        (lambda m: _import_ok(m))('.'.join(parts[:i]))
        for i in range(1, len(parts) + 1)
    ) if parts else False

def _import_ok(mod):
    import importlib
    try:
        importlib.import_module(mod); return True
    except ImportError:
        return False

Try / catch

try:
    target = _resolve_target(qualified_name)
except ImportError:
    log.error('module for %s not found — was it renamed?', qualified_name)
    raise

Prevention

When it happens

Trigger: Specifying a patch target like 'sglang.srt.nonexistent.module.fn' where no importable module exists at any prefix; also for a name with no dots at all, since a top-level module import is still attempted. Triggered via _apply_specs or in tests like test_resolve_nonexistent_raises.

Common situations: Renamed/moved modules after an sglang upgrade while patch YAML still references old paths; typos in patch spec files; circular-import or import-time errors inside the target module that surface as ImportError; missing optional dependency guarded imports.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/6172fb863a947a50. Report an issue: GitHub.