Code Interpreter 用于在 Sandbox 中执行代码并返回结构化结果。它适合数据分析、Notebook 类体验、Agent 生成代码后的即时运行,以及把表格、文本、错误信息和文件产物回传给业务系统。
SDK 入口
Code Interpreter 使用独立 SDK 包:
import { Sandbox } from "@e2b/code-interpreter";Python 使用:
from e2b_code_interpreter import Sandbox连接参数仍然使用 E2B_API_KEY、E2B_API_URL 和 E2B_DOMAIN。云沙箱内置 Code Interpreter 模板为 code-interpreter-v1,示例中建议显式指定该模板。
最小示例
import os
from e2b_code_interpreter import Sandbox
sandbox = Sandbox.create(
template="code-interpreter-v1",
api_key=os.environ["E2B_API_KEY"],
api_url=os.environ["E2B_API_URL"],
domain=os.environ["E2B_DOMAIN"],
)
try:
execution = sandbox.run_code("print('hello')")
print("".join(execution.logs.stdout or []).strip())
finally:
sandbox.kill()核心对象
sandbox.run_code()/sandbox.runCode():执行代码并返回执行结果。execution.logs.stdout/execution.logs.stderr:标准输出和标准错误。execution.text:最后一个裸表达式的文本结果。execution.results:富结果列表,可包含文本、表格等结构化结果。execution.error:运行时报错,包含错误类型、值和 traceback。Code Context:在多次执行之间保留变量、导入和工作目录。
后续阅读
该文章对您有帮助吗?