code-interpreter-v1 模板提供安全隔離的代碼執行沙箱環境,支援在雲端安全地執行 Python、JavaScript 等語言代碼,並保持跨調用的執行內容(變數、匯入、函數可跨調用引用)。
code-interpreter-v1 模板對齊 E2B Code Interpreter 的代碼上下文與代碼執行能力,可直接使用 E2B Code Interpreter SDK 訪問。
功能特性
| 特性 | 說明 |
| 多語言代碼執行 | 支援 Python 與 JavaScript,通過 run_code/runCode 執行 |
| 上下文保持 | 預設上下文中變數、匯入、函數跨調用保留 |
| 代碼上下文管理 | 支援建立、重啟、刪除獨立的代碼上下文,各上下文變數狀態互相隔離 |
| 檔案系統操作 | 支援上傳、下載、讀寫檔案,建立目錄、移動、刪除,覆蓋文本與二進位檔案 |
| 終端命令執行 | 支援同步命令執行與互動式終端(PTY) |
| 安全隔離 | 基於函數執行個體獨佔隔離,每個沙箱執行個體擁有獨立的檔案系統和進程空間 |
適用情境
| 情境 | 說明 |
| AI Agent 代碼沙箱 | 為 AI Agent 提供安全的代碼執行環境,防止不可信代碼訪問或篡改宿主系統資源 |
| 資料分析 | 在沙箱中運行 Python 資料分析指令碼,配合 pandas、numpy 等庫處理資料 |
| 檔案處理 | 上傳檔案到沙箱,執行格式轉換、資料清洗等操作後下載結果 |
| 指令碼執行與自動化 | 執行 Shell 命令、安裝依賴、運行自動化指令碼 |
預設配置
code-interpreter-v1 模板的預設配置如下:
| 配置項 | 預設值 | 說明 |
| 預設連接埠 | 5000 | 沙箱服務監聽連接埠 |
| CPU | 2 vCPU | 最低要求 |
| 記憶體 | 2048 MB | 最低要求 |
| 磁碟大小 | 15360 MB | — |
SDK 使用方式
使用 code-interpreter-v1 模板時,是否需要顯式指定 template 取決於 SDK:
| SDK | template 參數 | 說明 |
e2b_code_interpreter SDK |
不需要指定 | 專用 SDK 預設建立 code-interpreter-v1 沙箱 |
e2b SDK |
需要指定 code-interpreter-v1 |
通用 SDK 預設建立 base 沙箱,需要明確選取 code-interpreter-v1 模板 |
建立沙箱與執行代碼
使用 e2b_code_interpreter SDK 建立沙箱並通過 run_code 執行代碼:
from e2b_code_interpreter import Sandbox
sbx = Sandbox.create()
try:
execution = sbx.run_code("print('hello from code interpreter')")
print("".join(execution.logs.stdout))
finally:
sbx.kill()
TypeScript 樣本:
npm init -y
npm install @e2b/code-interpreter@^2.6.1 tsximport { Sandbox } from "@e2b/code-interpreter";
const sbx = await Sandbox.create();
try {
const execution = await sbx.runCode("print('hello from code interpreter')");
console.log(execution.logs.stdout.join(""));
} finally {
await sbx.kill();
}logs.stdout 與 logs.stderr 均為字串列表,需 "".join(...)(Python)或 .join("")(TypeScript)拼接為完整文本。
run_code / runCode 的主要參數:
| 參數 | 說明 |
code |
要執行的代碼 |
language |
執行語言,支援 python 與 javascript,未指定時預設 python;與 context 互斥 |
context |
指定在哪個代碼上下文中執行;與 language 互斥 |
timeout / timeoutMs |
代碼執行逾時(Python 單位為秒,預設 300 秒;TypeScript 單位為毫秒,預設 60000 毫秒) |
envs |
自訂環境變數 |
on_stdout / onStdout 等 |
流式回調,逐行接收 stdout/stderr/結果/錯誤 |
執行結果 Execution 包含 logs(stdout/stderr 列表)、results(末運算式結果,含 text 文本表示)、error(執行異常)和執行計數(Python 為 execution_count,TypeScript 為 executionCount)。
上下文保持
同一沙箱的預設上下文中,變數、匯入、函數跨調用保留:
sbx = Sandbox.create(**kwargs)
try:
sbx.run_code("x = 42")
execution = sbx.run_code("print(x)")
print("".join(execution.logs.stdout)) # 42
finally:
sbx.kill()
代碼上下文管理
每個代碼上下文(Context)擁有獨立的變數狀態。通過 context 參數將代碼路由到指定上下文執行;未指定 context 時使用預設上下文。上下文管理當前由 Python SDK 提供:
| 操作 | Python SDK 方法 |
| 建立上下文 | create_code_context(cwd="/home/user", language="python") |
| 重啟上下文 | restart_code_context(context) |
| 刪除上下文 | remove_code_context(context) |
| 在指定上下文執行 | run_code(code, context=ctx) |
run_code 的 context 參數需傳 Context 對象;restart_code_context / remove_code_context 可傳 Context 對象或內容識別碼 字串。create_code_context 需指定 language(python 或 javascript),cwd 預設為 /home/user。
from e2b_code_interpreter import Sandbox
sbx = Sandbox.create(**kwargs)
try:
# 建立獨立上下文
ctx = sbx.create_code_context(language="python", cwd="/home/user")
sbx.run_code("y = 100", context=ctx)
execution = sbx.run_code("print(y)", context=ctx)
print("".join(execution.logs.stdout)) # 100
# 預設上下文中無法訪問 ctx 的變數,execution.error 包含 NameError
default_execution = sbx.run_code("print(y)")
# 重啟上下文後變數被清空,execution.error 包含 NameError
sbx.restart_code_context(ctx)
restarted_execution = sbx.run_code("print(y)", context=ctx)
# 刪除上下文
sbx.remove_code_context(ctx)
finally:
sbx.kill()
TypeScript SDK 的 runCode 可正常執行代碼,但上下文管理方法(createCodeContext / listCodeContexts / restartCodeContext / removeCodeContext)當前在本平台不可用,如需管理獨立上下文請使用 Python SDK。
多語言執行
通過 language 參數指定執行語言,預設為 python:
sbx = Sandbox.create(**kwargs)
try:
execution = sbx.run_code("console.log('hello js')", language="javascript")
print("".join(execution.logs.stdout))
finally:
sbx.kill()const sbx = await Sandbox.create();
try {
const execution = await sbx.runCode("console.log('hello js')", { language: "javascript" });
console.log(execution.logs.stdout.join(""));
} finally {
await sbx.kill();
}使用通用 e2b SDK
使用 e2b SDK 需顯式指定模板 code-interpreter-v1,此時可使用沙箱的基礎能力(檔案、命令、進程),但不包含 run_code:
from e2b import Sandbox
sbx = Sandbox.create(template="code-interpreter-v1")
try:
result = sbx.commands.run("python --version")
print(result.stdout)
finally:
sbx.kill()
使用流程
-
建立沙箱執行個體:使用
e2b_code_interpreterSDK 調用Sandbox.create(),自動選擇code-interpreter-v1模板。 -
執行代碼:通過
run_code/runCode執行代碼;預設上下文自動保持變數狀態。 -
隔離執行:如需隔離變數狀態,使用
create_code_context建立獨立上下文,並通過context參數路由執行。 -
清理資源:完成後調用
kill()釋放沙箱;不再需要的上下文可用remove_code_context刪除。
沙箱執行個體狀態
沙箱執行個體在生命週期內經歷以下狀態:
| 狀態 | 說明 |
running |
就緒,可以使用 |
paused |
已暫停(深休眠),可恢複 |
terminated |
已終止 |
使用限制
| 限制項 | 約束 |
| 沙箱生命週期 | 單個沙箱執行個體最長生命週期為 24 小時(timeout 參數上限 86400 秒) |
| 空閑逾時 | 可通過 sandboxIdleTimeoutSeconds 參數設定,有效下限 60 秒(低於 60 秒按 60 秒生效) |
| 代碼執行逾時 | 單次 run_code / runCode 同步執行預設逾時 Python 300 秒、TypeScript 60000 毫秒,可通過 timeout / timeoutMs 調整 |
相關文檔
-
使用 Code Interpreter Sandbox(完整商務程序與生產建議)
-
base 模板(僅需 envd 基礎能力時選擇)
-
browser 模板(僅需瀏覽器自動化能力時選擇)
-
All-In-One 模板(需要同時使用瀏覽器和代碼執行能力時選擇)