How to beginner · 4 min read

E2B for data analysis agents

Quick answer
Use the e2b_code_interpreter.Sandbox class to create a secure Python sandbox for data analysis agents. Run code snippets, upload data files, and install packages dynamically within the sandbox environment to enable safe, isolated data processing.

PREREQUISITES

  • Python 3.8+
  • E2B API key (set as E2B_API_KEY environment variable)
  • pip install e2b-code-interpreter

Setup

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

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, upload your data file, run Python code for analysis, and retrieve the output safely.

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"])

# Upload a CSV data file to sandbox
with open("data.csv", "rb") as f:
    sandbox.files.write("data.csv", f.read())

# Run Python code to analyze data inside sandbox
code = '''
import pandas as pd

df = pd.read_csv("data.csv")
summary = df.describe().to_string()
print(summary)
'''

result = sandbox.run_code(code)
print("Analysis output:\n", result.text)

# Close sandbox session
sandbox.close()
output
Analysis output:
        col1       col2       col3
count  100.000000  100.00000  100.00000
mean    50.123456   49.98765   50.54321
std     29.123456   28.98765   29.54321
min      1.000000    2.00000    1.00000
25%     25.000000   24.50000   25.00000
50%     50.000000   50.00000   50.00000
75%     75.000000   75.50000   75.00000
max    100.000000  100.00000  100.00000

Common variations

You can run asynchronous code with asyncio support, install additional packages dynamically, or use streaming output for long-running analyses.

python
import os
import asyncio
from e2b_code_interpreter import Sandbox

async def async_analysis():
    sandbox = Sandbox(api_key=os.environ["E2B_API_KEY"])

    # Install pandas dynamically
    await sandbox.run_code("import subprocess; subprocess.run(['pip','install','pandas'])")

    # Upload data file
    with open("data.csv", "rb") as f:
        sandbox.files.write("data.csv", f.read())

    # Run analysis asynchronously
    code = '''
import pandas as pd

df = pd.read_csv("data.csv")
print(df.head())
'''
    result = await sandbox.run_code(code)
    print("Async analysis output:\n", result.text)

    sandbox.close()

asyncio.run(async_analysis())
output
Async analysis output:
    col1  col2  col3
0    1     2     3
1    4     5     6
2    7     8     9
3   10    11    12
4   13    14    15

Troubleshooting

  • If you see ModuleNotFoundError, ensure required packages are installed inside the sandbox using sandbox.run_code with pip install.
  • If file uploads fail, verify the file path and that sandbox.files.write is called before running code.
  • For authentication errors, confirm your E2B_API_KEY environment variable is set correctly.

Key Takeaways

  • Use Sandbox from e2b_code_interpreter to run isolated Python code for data analysis safely.
  • Upload data files directly into the sandbox environment with sandbox.files.write before executing analysis code.
  • Install any missing Python packages dynamically inside the sandbox using pip install commands.
  • Async execution is supported for non-blocking data processing workflows.
  • Always close the sandbox session with sandbox.close() to free resources.
Verified 2026-04
Verify ↗