You can create a customized version of the Code Interpreter sandbox (e.g., to add preinstalled packages) by following these steps:
Install dependencies:
pip install e2b dotenv
Define the template in a file (e.g., template.py) using the from_template method to inherit from the base code-interpreter-v1:
from e2b import Template
template = Template().from_template("code-interpreter-v1")
Create a build script (e.g., build.py) to execute the build process. You can specify an alias, cpu_count, memory_mb, and a logger:
from dotenv import load_dotenv
from .template import template
from e2b import Template, default_build_logger
load_dotenv()
Template.build(
template,
alias="code-interpreter-custom",
cpu_count=2,
memory_mb=2048,
on_build_logs=default_build_logger(),
)
Configure environment variables in a .env file:
E2B_API_KEY=e2b_***
Run the build:
python build.py
Use the custom template in your application by referencing the alias provided during the build:
from e2b import Sandbox
sbx = Sandbox.create(template="code-interpreter-custom")
execution = sbx.run_code("print('Hello, World!')")
print(execution.logs.stdout)