Pydantic-Monty
Pydantic-Monty provides Python bindings for the Monty sandboxed Python interpreter, a Rust-based virtual machine designed for securely executing untrusted Python code. It is currently at version 0.0.12 and sees frequent, rapid releases as it is under active development, indicating ongoing feature additions and potential API changes.
Common errors
-
ModuleNotFoundError: No module named 'requests'
cause Attempting to import a Python module (e.g., 'requests', 'numpy', or many standard library modules like 'os', 'sys') that is not allowed or built-in within the Monty sandboxed environment.fixVerify that the module you are trying to import is either a Monty built-in or has been explicitly allowed/provided through VM configuration. Remember that Monty is a restricted environment. -
VmException: ValueError('Cannot mount: directory does not exist: /path/to/non_existent_dir')cause Trying to mount a host directory into the Monty VM using `vm.mount()` where the specified host directory path does not exist on the underlying file system.fixEnsure that the first argument to `vm.mount('host_path', 'vm_path')` points to an existing and accessible directory on the host machine before attempting to mount it. -
VmException: RecursionError: maximum recursion depth exceeded
cause Code executed inside the Monty VM exceeded the configured recursion depth limit, typically due to an infinite recursion or deeply nested function calls.fixRefactor the Python code running in the VM to avoid excessive recursion. If deeply recursive algorithms are intentional, review Monty's documentation for any configuration options to adjust the recursion limit (if available and safe to do so).
Warnings
- gotcha Monty's VM is a sandboxed environment, meaning it has restricted access to many standard Python modules (e.g., `os`, `sys`, networking) and system resources by default. Code running within Monty cannot access the host filesystem or network unless explicitly configured (e.g., via `mount`).
- gotcha As of v0.0.12, the `zip()` built-in function in Monty enforces strict mode, raising a `ValueError` if iterables passed to it have different lengths. This differs from standard Python 3's `zip()`, which truncates to the shortest iterable.
- breaking The library is in early development (`0.0.x` versioning), which means APIs are subject to change without strict adherence to semantic versioning. Breaking changes may occur in minor or patch releases.
Install
-
pip install pydantic-monty
Imports
- Monty
from monty import Monty
- VmException
from monty import VmException
Quickstart
from monty import Monty, VmException
# Initialize the Monty VM
vm = Monty()
# Run some simple Python code inside the VM
result = vm.run('x = 10\ny = 20\nprint(x + y)')
print(f'stdout: {result.stdout.strip()!r}')
print(f'stderr: {result.stderr.strip()!r}')
print(f'return_value: {result.return_value!r}')
# Example of handling a VM-level exception
try:
vm.run('raise ValueError("Something went wrong in the sandbox!")')
except VmException as e:
print(f'VM error caught: {e}')