E2B is an open-source infrastructure that provides isolated cloud sandboxes for AI agents to safely execute code, process data, and run tools. The Python SDK, currently at version 2.20.0, enables starting and managing these environments. Releases are frequent, often weekly or bi-weekly, addressing minor changes, patch fixes, and new features.
pip install e2bVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create an E2B sandbox, run a shell command inside it, and print the output. It highlights the importance of setting the E2B_API_KEY environment variable for authentication.
Replace `sandbox = Sandbox()` with `sandbox = Sandbox.create()`.
Migrate file write operations from older patterns to `sandbox.files.write(path, content)` or `sandbox.files.write_files([{'path': ..., 'data': ...}])`.Sign up on the E2B dashboard, obtain your API key, and set it as an environment variable (`export E2B_API_KEY=e2b_***`) or pass it as an argument (`Sandbox.create(api_key='e2b_***')`).
Install `e2b-code-interpreter` separately if `run_code()` is needed. Pay attention to the import statements: `from e2b import Sandbox` for core SDK, and `from e2b_code_interpreter import Sandbox` for code interpretation.
Rebuild older custom templates or temporarily disable secure communication by setting `secure=False` during sandbox creation (not recommended for production). Check template `envd` version using `e2b template list` CLI command.
Replace `from e2b_code_interpreter import CodeInterpreter` with `from e2b_code_interpreter import Sandbox` (if using the code interpreter specific package) or `from e2b import Sandbox` (for the general SDK), and use `Sandbox.create()` to initialize the sandbox.
Ensure your E2B API key is correctly set as an environment variable named `E2B_API_KEY` or passed explicitly when creating the sandbox, for example: `sandbox = Sandbox.create(api_key='YOUR_API_KEY')`.
Increase the sandbox timeout when creating it (e.g., `Sandbox.create(timeout=60)`), ensure your custom template is built on a `code-interpreter` base image, and verify the `setStartCmd` is correct if building from a Docker image.
Explicitly call the `.read()` or `.aread()` (for async) method on the command result object to ensure the entire output stream is consumed before attempting to access its content. For example: `result = sandbox.commands.run('ls -l'); output = result.read().stdout`