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
Environment Variables
Complete Example
Best Practices
Use contexts for isolation
Use contexts for isolation
Create separate contexts for unrelated execution tasks to prevent variable conflicts:
Always clean up contexts
Always clean up contexts
Delete contexts when finished to free resources:
Set reasonable timeouts
Set reasonable timeouts
Always set timeouts to prevent runaway code execution:
Use streaming for long operations
Use streaming for long operations
For long-running code, use callbacks to monitor progress:
Execution Response
Limitations
Related Resources
Process Execution
Execute code in other languages (TypeScript, JavaScript)
PTY Sessions
Interactive terminal sessions for Python REPL