Skip to main content

PythonEnv

Sandbox-backed environment exposing a persistent Python REPL for code execution.

Overview

PythonEnv provides a stateful Python interpreter running in a sandboxed container:
  • Persistent state: Variables and imports persist across code executions
  • IPython-like behavior: Trailing expressions are automatically printed
  • Package management: Pre-install packages via pip during setup
  • Error handling: Full traceback capture for debugging
  • Execution tracking: Numbered outputs like Jupyter notebooks

Inheritance

Constructor

Parameters

str
default:"numpy sympy scipy"
Space-separated list of packages to install with pip during sandbox startup. Use empty string to skip installation.
int
default:"30"
Maximum time to wait for the Python worker to be ready.
All other parameters are inherited from SandboxEnv:
  • sandbox_name, docker_image, cpu_cores, memory_gb, etc.
  • timeout_per_command_seconds - Timeout for each Python execution

Tools

python

Execute Python code in the persistent REPL.
str
Python code to execute. Can be multiple lines.
Returns: str - Formatted output including stdout, stderr, and expression results. Output format:
  • stdout content (if any)
  • stderr content prefixed with “stderr:” (if any)
  • Exception tracebacks (on error)
  • Expression results as Out[N]: <repr> (for trailing expressions)
  • (no output) if nothing was produced

State Management

PythonEnv adds Python-specific state to the base sandbox state:

Built-in Rubric

PythonEnv includes PythonMonitorRubric which tracks:
  • python_ready_wait_time: Time spent waiting for Python worker initialization
  • All metrics from SandboxEnv

Example Usage

Basic Math Environment

Scientific Computing

Data Analysis

Code Verification with Tests

Custom Package Installation

REPL Behavior

The Python REPL behaves like IPython:

Implementation Details

Worker Process

PythonEnv runs a background Python worker process in the sandbox that:
  1. Creates named pipes (FIFOs) for bidirectional communication
  2. Maintains a persistent namespace across executions
  3. Executes code using exec() for statements and eval() for trailing expressions
  4. Captures stdout/stderr using contextlib.redirect_stdout/stderr
  5. Returns results as JSON over the response pipe

Startup Sequence

  1. Sandbox container starts with Python 3.11
  2. Packages are installed via pip (if specified)
  3. Worker script is uploaded and launched as background process
  4. Worker creates communication pipes and sets ready flag
  5. First python() call waits for ready flag before executing
  6. Subsequent calls execute immediately

Error Types

  • PythonWorkerNotReadyError: Worker failed to start within timeout
  • PythonWorkerRequestError: Communication error with worker
  • PythonWorkerDeadError: Worker process died unexpectedly
All inherit from vf.SandboxError.

When to Use

Use PythonEnv for:
  • Math and scientific computing tasks
  • Code generation with execution verification
  • Multi-step computations requiring persistent state
  • Algorithm development and testing
  • Data analysis workflows
Use SandboxEnv directly for:
  • Bash commands and system operations
  • Multi-language environments
  • File system operations
  • Custom execution environments
Use ToolEnv for:
  • Stateless Python function calls
  • Tasks not requiring code execution
  • Pre-defined operations only

See Also