This guide covers how to use environment variables in sandboxes.Documentation Index
Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
Use this file to discover all available pages before exploring further.
Help us improve CoreWeave documentation. Take the docs survey.
Set and manage environment variables in CWSandbox instances, including session-level defaults
This guide covers how to use environment variables in sandboxes.Documentation Index
Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
Use this file to discover all available pages before exploring further.
from cwsandbox import Sandbox
with Sandbox.run(
environment_variables={"LOG_LEVEL": "info"},
) as sandbox:
result = sandbox.exec([
"python",
"-c",
"import os; print(os.environ.get('LOG_LEVEL'));",
]).result()
print(result.stdout.strip()) # "info"
from cwsandbox import SandboxDefaults, Session
defaults = SandboxDefaults(
environment_variables={
"PROJECT_ID": "my-project",
"LOG_LEVEL": "info",
},
)
with Session(defaults) as session:
with session.sandbox() as sb1:
result = sb1.exec([
"python",
"-c",
"import os; print(os.environ.get('LOG_LEVEL'));",
]).result()
print(result.stdout.strip()) # "info"
# Override LOG_LEVEL and add new variable
with session.sandbox(
environment_variables={
"LOG_LEVEL": "debug", # Override session default
"MODEL_NAME": "gpt-4", # Add new variable
}
) as sb2:
result = sb2.exec([
"python",
"-c",
"import os; "
"print(os.environ.get('PROJECT_ID')); "
"print(os.environ.get('LOG_LEVEL')); "
"print(os.environ.get('MODEL_NAME'));",
]).result()
lines = result.stdout.strip().split("\n")
print(lines) # ["my-project", "debug", "gpt-4"]
with Session(defaults) as session:
@session.function(environment_variables={"MODEL_VERSION": "v2.0"})
def process(task_id: int) -> dict:
import os
return {
"task": task_id,
"project": os.environ.get("PROJECT_ID"), # From session defaults
"version": os.environ.get("MODEL_VERSION"), # From function decorator
}
result = process.remote(42).result()
print(result) # {"task": 42, "project": "my-project", "version": "v2.0"}
env_vars = {"MODEL_VERSION": "v2.0"}
@session.function(environment_variables=env_vars)
def process(task_id: int) -> dict:
import os
return {"version": os.environ.get("MODEL_VERSION")}
result = process.remote(42).result() # version: "v2.0"
env_vars["MODEL_VERSION"] = "v3.0" # Mutate the dictionary
result = process.remote(42).result() # version: "v3.0" (changed)
Was this page helpful?