How to beginner · 3 min read

How to execute Python code with E2B

Quick answer
Use the e2b_code_interpreter package to create a Sandbox instance with your API key, then call run_code() to execute Python code securely. You can also write files to the sandbox and run pip installs inside it.

PREREQUISITES

  • Python 3.8+
  • E2B API key
  • pip install e2b-code-interpreter

Setup

Install the e2b-code-interpreter package and set your E2B API key as an environment variable.

  • Install package: pip install e2b-code-interpreter
  • Set API key in your shell: export E2B_API_KEY='your_api_key_here'
bash
pip install e2b-code-interpreter
output
Collecting e2b-code-interpreter
  Downloading e2b_code_interpreter-1.0.0-py3-none-any.whl (15 kB)
Installing collected packages: e2b-code-interpreter
Successfully installed e2b-code-interpreter-1.0.0

Step by step

Create a Sandbox instance with your API key, then run Python code using run_code(). You can also write files to the sandbox and install packages.

python
import os
from e2b_code_interpreter import Sandbox

# Initialize sandbox with API key from environment
sandbox = Sandbox(api_key=os.environ["E2B_API_KEY"])

# Run simple Python code
execution = sandbox.run_code("print('Hello from sandbox')")
print(execution.text)

# Write a file to sandbox
sandbox.files.write("data.txt", b"Sample data content")

# Run code that reads the file
code = """
with open('data.txt', 'r') as f:
    content = f.read()
print('File content:', content)
"""
result = sandbox.run_code(code)
print(result.text)

# Install a package inside sandbox
sandbox.run_code("import subprocess; subprocess.run(['pip', 'install', 'requests'])")

# Close sandbox when done
sandbox.close()
output
Hello from sandbox
File content: Sample data content
Collecting requests
  Downloading requests-2.31.0-py3-none-any.whl (62 kB)
Installing collected packages: requests
Successfully installed requests-2.31.0

Common variations

  • Use run_code() asynchronously if supported by your environment.
  • Run multi-line scripts by passing a string with triple quotes.
  • Manage sandbox files with sandbox.files.write() and sandbox.files.read().
  • Use sandbox.close() to release resources after execution.

Troubleshooting

  • If you see authentication errors, verify your E2B_API_KEY environment variable is set correctly.
  • For package install failures, ensure network access is enabled in your sandbox environment.
  • If run_code() hangs, check your code for infinite loops or blocking calls.
  • Always call sandbox.close() to avoid resource leaks.

Key Takeaways

  • Use Sandbox from e2b_code_interpreter to securely run Python code remotely.
  • Manage files inside the sandbox with sandbox.files.write() and sandbox.files.read().
  • Always close the sandbox with sandbox.close() to free resources.
  • You can install Python packages inside the sandbox by running pip commands via run_code().
  • Set your API key securely in the environment variable E2B_API_KEY before running code.
Verified 2026-04
Verify ↗