RestrictedPython
RestrictedPython is a defined subset of the Python language which allows to provide a program input into a trusted environment.
What it is and what it does
RestrictedPython is a code compiler that transforms Python source into a restricted subset suitable for executing untrusted code in a controlled environment. It intercepts dangerous operations—such as arbitrary imports, unsafe attribute access, and format string exploits—by rewriting the compiled bytecode to route these operations through guarded functions that you control. The package is not a sandbox or a security guarantee; rather, it provides the machinery to define a trusted execution context and enforce restrictions within it.
You supply Python source code, compile it with compile_restricted(), and execute the resulting bytecode in a namespace you define (typically using safe_globals as a baseline). RestrictedPython prevents access to internals like __import__, restricts attribute traversal via safer_getattr, and blocks syntax constructs deemed unsafe (such as try/except* in recent versions). It has been maintained since 2007 and currently supports Python 3.10 through 3.14 on CPython only.
Use it for:
- Execute user-supplied scripts or formulas in a web application without granting access to the filesystem or system calls.
- Provide a templating or rule-engine system where end users can write logic without risking the host application.
- Sandbox third-party plugins or extensions to prevent them from accessing sensitive APIs or data.
- Implement a restricted REPL or notebook environment for educational or demonstration purposes.
- Validate and restrict Python code before passing it to a workflow or job queue system.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
RestrictedPython compiles and executes a restricted subset of Python code in a controlled environment, preventing access to dangerous operations like arbitrary imports and unsafe attribute access.
Yes, if you need to execute untrusted Python code with controlled restrictions. RestrictedPython is mature, actively maintained, and has no known vulnerabilities. However, it is not a complete sandbox—it requires careful design of your safe_globals and guard functions to be effective. Verify the ZPL-2.1 license compatibility for your use case, and confirm that CPython-only support meets your deployment requirements.
Install
restrictedpython on PyPI
pip
pip install restrictedpythonuv
uv add restrictedpythonpoetry
poetry add restrictedpythonInstalling RestrictedPython
Before you install
Low install friction with no runtime dependencies. The package is actively maintained with a recent release (35 days ago) and has been in development since 2007. Supports current Python versions (3.10–3.14) on CPython only.
License in practice
Licensed under ZPL-2.1 (Zope Public License 2.1), but license treatment is marked unclear in the metadata—verify the exact terms before use in proprietary or commercial contexts.
Quickstart
from RestrictedPython import compile_restricted, safe_globals
source = "def greet():\n return 'Hello'"
byte_code = compile_restricted(source, '', 'exec')
exec(byte_code, safe_globals, {})
RestrictedPython only supports CPython; it does not work with PyPy or other Python implementations.
Verify before relying
- Whether the ZPL-2.1 license is compatible with your project's licensing requirements.
- Whether the 'unclear' license treatment indicates any known ambiguity in the license terms.
- Whether the package's security restrictions are sufficient for your threat model (it is not a full sandbox).
Package facts
| License | ZPL-2.1 (unclear) |
| Python support | supports the current Python release (<3.16,>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 35 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 8,872,727/month — #1,584 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: restrictedpython-8.4-py3-none-any.whl
Keywords: restricted, execution, security, untrusted, code
Tags
More Security packages
Provides Python bindings to the FreeDesktop.org…
permissive · top 1,000 on PyPI
msalMSAL for Python handles OAuth2 and OpenID…
permissive · top 1,000 on PyPI
joserfcjoserfc implements JOSE standards (JWS, JWE,…
permissive · top 1,000 on PyPI
AuthlibAuthlib provides a complete implementation of…
permissive · top 1,000 on PyPI
argon2-cffi-bindingsProvides low-level CFFI bindings to the…
permissive · top 1,000 on PyPI
adalADAL for Python authenticates applications with…
permissive · top 1,000 on PyPI
starlark-pyo3Exposes the Starlark interpreter (a Python-like…
unclear · top 15,000 on PyPI
AccessControlAccessControl provides a security framework for…
unclear · top 15,000 on PyPI
nono-pynono-py provides Python bindings for…
permissive · top 15,000 on PyPI
e2bE2B provides a Python SDK to create and control…
permissive · top 5,000 on PyPI
astevalAsteval is a safe Python expression and…
permissive · top 5,000 on PyPI
pydantic-monty-runtimeProvides the `monty` command-line binary, a…
permissive · top 15,000 on PyPI
hyperlight-sandboxHyperlight-sandbox provides a Python API for…
permissive · top 15,000 on PyPI
zope.deferredimportDefers module imports until they are actually…
unclear · top 15,000 on PyPI
llm-sandboxExecutes code generated by large language…
unclear · top 15,000 on PyPI
zope.proxyzope.proxy provides transparent wrapper objects…
unclear · top 15,000 on PyPI