Skip to main content
The Code Interpreter provides advanced Python code execution capabilities with context isolation, streaming output, and comprehensive error handling.

Overview

Daytona’s Code Interpreter enables:
  • Stateful execution - Maintain variables and state between executions
  • Context isolation - Run code in isolated interpreter contexts
  • Real-time streaming - Stream stdout/stderr as code executes
  • Error tracking - Detailed error information with tracebacks
For other languages (TypeScript, JavaScript), use sandbox.process.codeRun() instead. See Process Execution for details.

Basic Execution

Simple Code Execution

Execute Python code and get results:

Execution with Timeout

Set a timeout to prevent long-running code:
string
required
Python code to execute
RunCodeOptions
Execution options

Stateful Execution

Default Context State

Variables persist between executions in the default context:

Context Isolation

Create Isolated Context

Create separate contexts for isolated execution:

Context with Working Directory

List Active Contexts

string
Working directory for the context. Uses sandbox working directory if omitted.

Streaming Output

Real-time Output Streaming

Stream output as code executes:

Error Handling

Capture Execution Errors

Error Response:

Environment Variables

Complete Example

Best Practices

Create separate contexts for unrelated execution tasks to prevent variable conflicts:
Delete contexts when finished to free resources:
Always set timeouts to prevent runaway code execution:
For long-running code, use callbacks to monitor progress:

Execution Response

Limitations

The Code Interpreter currently only supports Python. For TypeScript and JavaScript code execution, use sandbox.process.codeRun() instead.
Default timeout is 10 minutes. Set timeout: 0 for no timeout, but be cautious of infinite loops.

Process Execution

Execute code in other languages (TypeScript, JavaScript)

PTY Sessions

Interactive terminal sessions for Python REPL