code-interpreter icon indicating copy to clipboard operation
code-interpreter copied to clipboard

Fixes env vars in languages other than Python

Open mishushakov opened this issue 7 months ago • 3 comments

Changelog:

  • closes https://github.com/e2b-dev/E2B/issues/665
  • runs a code cell to set env variables if any env variables are set (either sandbox-level or per cell)
  • note: os.environ.set_envs_for_execution function might no longer be required?

Example:

import { Sandbox } from "@e2b/code-interpreter";

const sbx = await Sandbox.create({ debug: true });

const code = await sbx.runCode(`console.log(process.env.TEST);`, {
  language: "javascript",
  envs: { TEST: "hello" },
});

console.log(code.logs);

Before:

{
  stdout: [ "undefined\n" ],
  stderr: [],
}

After:

{
  stdout: [ "hello\n" ],
  stderr: [],
}

mishushakov avatar May 14 '25 19:05 mishushakov

@mishushakov can you add tests for this case? it would be good to catch if setting env var breaks

mlejva avatar May 15 '25 19:05 mlejva

yes, these will be in a separate PR as otherwise we would be unable to merge this (the CI is using deployed template version)

mishushakov avatar May 15 '25 19:05 mishushakov

Let's wait for #116 here

mishushakov avatar Jun 04 '25 12:06 mishushakov

I need to rewrite the tests. I don't know what went south last time, but they don't look correct to me.

mishushakov avatar Jun 06 '25 16:06 mishushakov

Env vars set during build:

Screenshot 2025-06-06 at 19 30 49

mishushakov avatar Jun 06 '25 17:06 mishushakov

Env vars set during build:

I don't think this is correct, try this:A

from e2b_code_interpreter import Sandbox

sbx = Sandbox(envs={"TEST": "ABC"})
execution = sbx.run_code(
    """
import os
os.getenv("TEST")
"""
)
print(execution.results[0])

jakubno avatar Jun 10 '25 07:06 jakubno