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.
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.
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.
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 usingsandbox.run_codewithpip install. - If file uploads fail, verify the file path and that
sandbox.files.writeis called before running code. - For authentication errors, confirm your
E2B_API_KEYenvironment variable is set correctly.
Key Takeaways
- Use
Sandboxfrome2b_code_interpreterto run isolated Python code for data analysis safely. - Upload data files directly into the sandbox environment with
sandbox.files.writebefore executing analysis code. - Install any missing Python packages dynamically inside the sandbox using
pip installcommands. - Async execution is supported for non-blocking data processing workflows.
- Always close the sandbox session with
sandbox.close()to free resources.