{"id":2016,"library":"e2b","title":"E2B SDK","description":"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.","status":"active","version":"2.20.0","language":"en","source_language":"en","source_url":"https://github.com/e2b-dev/e2b/tree/main/packages/python-sdk","tags":["AI agents","cloud environments","sandbox","code execution","development environment"],"install":[{"cmd":"pip install e2b","lang":"bash","label":"Core SDK"},{"cmd":"pip install e2b-code-interpreter","lang":"bash","label":"Code Interpreter SDK (for run_code)"}],"dependencies":[{"reason":"Required Python version.","package":"python","version":">=3.10, <4.0","optional":false},{"reason":"Required for `run_code()` functionality (e.g., executing arbitrary Python code within the sandbox).","package":"e2b-code-interpreter","optional":true}],"imports":[{"note":"Use this for general sandbox management (commands, files, lifecycle).","symbol":"Sandbox","correct":"from e2b import Sandbox"},{"note":"Use this specifically when you need `run_code()` functionality. Requires the `e2b-code-interpreter` package. In v2, the synchronous SDK's primary Sandbox class is imported from here.","symbol":"Sandbox","correct":"from e2b_code_interpreter import Sandbox"}],"quickstart":{"code":"import os\nfrom e2b import Sandbox\n\n# Ensure E2B_API_KEY is set in your environment\n# You can get your API key from the E2B dashboard: https://e2b.dev/docs/quickstart/running-your-first-sandbox\napi_key = os.environ.get('E2B_API_KEY', '')\nif not api_key:\n    print(\"Warning: E2B_API_KEY environment variable not set. Sandbox creation may fail or default to a limited scope.\")\n\nwith Sandbox.create(api_key=api_key) as sandbox:\n    print(f\"Sandbox created with ID: {sandbox.sandbox_id}\")\n    result = sandbox.commands.run('echo \"Hello from E2B!\"')\n    print(f\"Stdout: {result.stdout}\")\n    if result.stderr:\n        print(f\"Stderr: {result.stderr}\")\n\nprint(\"Sandbox session ended.\")","lang":"python","description":"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."},"warnings":[{"fix":"Replace `sandbox = Sandbox()` with `sandbox = Sandbox.create()`.","message":"The method for creating a Sandbox instance changed in v2. Instead of direct instantiation `Sandbox()`, you must now use the class method `Sandbox.create()`.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Migrate file write operations from older patterns to `sandbox.files.write(path, content)` or `sandbox.files.write_files([{'path': ..., 'data': ...}])`.","message":"File operation APIs were updated in v2 for consistency. Single file writes now use `sandbox.files.write()`, and multiple file writes use `sandbox.files.write_files()`.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"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_***')`).","message":"An E2B API Key is required for most operations and must be provided, typically via the `E2B_API_KEY` environment variable or explicitly passed during sandbox creation. Failing to provide it will result in errors or limited functionality.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"There are two distinct Python packages for E2B: `e2b` (for general sandbox management and commands) and `e2b-code-interpreter` (specifically for `run_code()` functionality). Both can export a `Sandbox` class, leading to potential import confusion if not installed and used correctly.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"In v2, sandboxes are secure by default. If you are using custom templates created before `envd v0.2.0`, you might need to rebuild them to enable secure communication, or you may encounter errors.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}