sgl-project/sglang · error · ImportError

could not create an import spec for {module_name} at {path}

Error message

could not create an import spec for {module_name} at {path}

What it means

importlib.util.spec_from_file_location returned an unusable spec (None or spec.loader None) for the built extension file, so it cannot be imported by path.

Source

Thrown at python/sglang/srt/rust_extensions/loader.py:402

            os.fsync(destination_file.fileno())
        temporary_path.chmod(0o755)
        os.replace(temporary_path, destination)
        directory_descriptor = os.open(destination.parent, os.O_RDONLY)
        try:
            os.fsync(directory_descriptor)
        finally:
            os.close(directory_descriptor)
    finally:
        temporary_path.unlink(missing_ok=True)


def _load_extension_from_path(module_name: str, path: Path) -> ModuleType:
    loaded = sys.modules.get(module_name)
    if loaded is not None:
        return loaded
    module_spec = importlib.util.spec_from_file_location(module_name, path)
    if module_spec is None or module_spec.loader is None:
        raise ImportError(
            f"could not create an import spec for {module_name} at {path}"
        )
    module = importlib.util.module_from_spec(module_spec)
    sys.modules[module_name] = module
    try:
        module_spec.loader.exec_module(module)
    except BaseException:
        sys.modules.pop(module_name, None)
        raise
    return module

View on GitHub (pinned to 0132848349)

Solutions

  1. Verify the file exists, is non-empty, and ends with sysconfig's EXT_SUFFIX
  2. Rebuild the artifact
  3. Import with a matching Python version/ABI
Defensive patterns

Strategy: try-catch

Validate before calling

import importlib.util, sysconfig
p = str(path)
assert p.endswith(sysconfig.get_config_var("EXT_SUFFIX")) and path.stat().st_size > 0

Try / catch

try:
    _load_extension_from_path(name, path)
except ImportError as e:
    rebuild_and_retry(name, path)

Prevention

When it happens

Trigger: Loading the staged .so/.pyd file on a platform where the suffix has no registered import loader, or a corrupt/empty file.

Common situations: Renamed extension file with unsupported suffix; truncated build artifact; mismatched Python ABI making the loader unavailable.

Related errors


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