E2B Code Interpreter 預設執行 Python。雲沙箱當前支援通過 language 參數選擇 JavaScript、TypeScript 和 Bash;不支援 Java 和 R。
Python
Python 是預設語言,不需要顯式傳入 language:
execution = sandbox.run_code('print("Hello, world!")')
print("".join(execution.logs.stdout or []).strip())TypeScript 樣本:
const execution = await sandbox.runCode('print("Hello, world!")');
console.log(execution.logs.stdout.join("").trim());JavaScript 和 TypeScript
JavaScript 使用 javascript 或 js,TypeScript 使用 typescript 或 ts:
context = sandbox.create_code_context(
cwd="/home/user",
language="typescript",
request_timeout=60_000,
)
execution = sandbox.run_code(
"""
const value = await Promise.resolve(42)
console.log(value)
""",
context=context,
)TypeScript SDK 中使用同名 language 選項:
const execution = await sandbox.runCode(
`
const value = await Promise.resolve(42)
console.log(value)
`,
{ language: "typescript" },
);E2B 官方文檔說明 TypeScript 支援 top-level await、ESM-style import 和 Promise 自動解析。雲沙箱接入時應先驗證模板內 Node.js 版本和依賴安裝能力。
Bash
其他語言通過對應的 language 值選擇:
sandbox.run_code('echo "Hello from Bash"', language="bash")TypeScript 樣本:
await sandbox.runCode('echo "Hello from Bash"', { language: "bash" });選擇建議
資料分析和圖表產生優先使用 Python。
需要 npm 包、ESM import 或 TypeScript 文法時使用 JavaScript / TypeScript,並先安裝依賴。
簡單系統命令優先使用 Commands API;需要在 Code Interpreter 上下文中保留執行狀態時再使用
language="bash"。Java 代碼請改用 Commands API 在模板內執行
javac/java,或使用預裝 Java 運行時的自訂模板。R 代碼請改用 Commands API 在模板內執行
Rscript,或使用預裝 R 運行時的自訂模板。生產接入前,為每種語言準備最小驗證指令碼,並確認 stdout、stderr、
execution.results和錯誤返回都符合預期。