支持语言

更新时间:
复制 MD 格式

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 使用 javascriptjs,TypeScript 使用 typescriptts

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 和错误返回都符合预期。