RestrictedPython is a tool that defines a subset of the Python language, allowing program input to be executed within a trusted environment. It is not a full sandbox system but aids in establishing a controlled execution space for untrusted code. The current stable version is 8.1, released on 2025-10-19, and the project maintains an active release cadence.
pip install RestrictedPythonVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates compiling and executing a simple Python function within a restricted environment. It uses `compile_restricted` to process the source code and `exec` with a modified `safe_globals` dictionary to control available built-ins and attributes. You can extend `restricted_globals` to whitelist specific functions or modules as needed for your application.
Users must implement robust policies for operations like `_print_`, `_write_`, `_getattr_`, `_getitem_`, and strictly curate the `__import__` hook and `__builtins__` dictionary to prevent escapes. `safe_globals` is a starting point, not a complete solution.
Ensure your execution environment uses CPython.
Upgrade to RestrictedPython 8.0 or higher. If unable to upgrade, consider downgrading Python to 3.10 or lower, where `try/except*` is not available.
Update code that unpacks the return value of `compile_restricted` calls. Instead of `code, errors = compile_restricted(...)`, use `result = compile_restricted(...)` and access `result.code`, `result.errors`, `result.warnings`, `result.used_names`.
Remove usage of the `Ellipsis` statement in restricted code.
Consult the `requires_python` metadata on PyPI or the official documentation's 'Supported Python versions' section to ensure compatibility with your Python interpreter.
No dependency data recorded yet.