--- id: restrictedpython version: "8.4" license: ZPL-2.1 license_treatment: unclear maintenance: active --- # RestrictedPython — RestrictedPython is a defined subset of the Python language which allows to provide a program input into a trusted environment. License: unclear · Maintenance: active · Downloads: 8.9M/mo ## 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 above — 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 pip install restrictedpython uv add restrictedpython poetry add restrictedpython ## Installing 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_current - Install friction: low - Maintenance: active - Downloads: 8.9M/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags restricted code execution, sandboxed python execution, untrusted code isolation, safe python subset, code compilation restrictions, python security sandbox, code-execution, sandboxing, untrusted-code [View on SkillFed](https://skillfed.io/packages/restrictedpython) · [View on PyPI](https://pypi.org/project/restrictedpython/)