xtekky/gpt4free · error · ImportError

'{name}' is not available in the restricted os shim.

Error message

'{name}' is not available in the restricted os shim.

What it means

Raised inside the g4f MCP '.pa.py' sandbox when code does 'import os.<submodule>' (e.g. 'os.path') and the submodule attribute does not exist on the restricted os shim. The shim (_make_restricted_os in g4f/mcp/pa_provider.py:384) only exposes urandom, name, sep, linesep, altsep and pathsep, so almost every dotted os import fails. This is an intentional security restriction: filesystem, process and environment operations are unavailable in user-provided PA provider files.

Source

Thrown at g4f/mcp/pa_provider.py:460

    )

    def _restricted_import(name, globals=None, locals=None, fromlist=(), level=0):
        if level > 0:
            raise ImportError(
                "Relative imports are not allowed inside a .pa.py sandbox."
            )
        base = name.split(".")[0]
        # Return the restricted os shim instead of the real os module.
        if base == "os":
            _os_shim = _make_restricted_os()
            if name == "os":
                return _os_shim
            # Handle "os.submodule" — try to resolve from the shim
            obj = _os_shim
            for part in name.split(".")[1:]:
                obj = getattr(obj, part, None)
                if obj is None:
                    raise ImportError(
                        f"'{name}' is not available in the restricted os shim."
                    )
            return obj
        if base not in allowed:
            # Before rejecting, check if it's a workspace module (sibling .py file).
            workspace = get_workspace_dir()
            ws_module = _load_workspace_module(
                base, workspace, globals, fromlist, level
            )
            if ws_module is not None:
                # Handle submodule imports (e.g. "pkg.sub")
                if name != base:
                    # Try to resolve the full dotted path from the loaded module
                    obj = ws_module
                    for part in name.split(".")[1:]:
                        obj = getattr(obj, part, None)
                        if obj is None:
                            raise ImportError(

View on GitHub (pinned to 973504e177)

Solutions

  1. Use plain 'import os' and only the attributes urandom, name, sep, linesep, altsep, pathsep
  2. Replace os.path.join with pathlib.Path (pathlib is in SAFE_MODULES)
  3. Replace os.environ lookups with values passed as provider parameters
  4. If you maintain the server and truly need more of os, extend _SAFE_OS_ATTRS in _make_restricted_os — but each addition widens the sandbox attack surface

Example fix

// before
import os.path
p = os.path.join(base, "file.json")

// after
from pathlib import Path
p = Path(base) / "file.json"
Defensive patterns

Strategy: validation

Validate before calling

# before writing the .pa.py, check the imports you plan to use
ALLOWED_OS_ATTRS = {"urandom", "name", "sep", "linesep", "altsep", "pathsep"}
assert all(a in ALLOWED_OS_ATTRS for a in os_attrs_used), "use plain 'import os' only"

Try / catch

try:
    import os.subthing  # will raise ImportError in sandbox
except ImportError as e:
    if "restricted os shim" in str(e):
        # rewrite the import: plain 'import os' or pathlib
        ...

Prevention

When it happens

Trigger: Executing a .pa.py file whose top-level code contains 'import os.path', 'import os.environ' or any 'import os.X' where X is not one of the six shim attributes. Only 'import os' followed by use of os.urandom/os.name/os.sep/os.linesep/os.altsep/os.pathsep succeeds.

Common situations: A provider script copied from a normal Python project that uses os.path.join or os.environ; porting example code that reads environment variables for API keys; any sandbox script that tries to detect the platform via dotted import instead of attribute access.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/d76b6ec2aa268170. Report an issue: GitHub.