Update environment variables by merging new values with existing ones. This preserves existing variables while adding or updating specific ones.
Overview
Updating environment variables is ideal for:
- Adding new variables without removing existing ones
- Updating specific variables while preserving others
- Incrementally building environment configuration
- Modifying configuration without full replacement
update() merges new variables with existing ones. Existing variables not specified in the update are preserved. This is safer than set_all() which replaces everything.
Update Single Variable
Update a single environment variable:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Set initial variables
sandbox.env.set_all({
"API_KEY": "sk-123",
"DEBUG": "true"
})
# Update a single variable
sandbox.env.update({"DEBUG": "false"})
# Verify update
env = sandbox.env.get_all()
print(f"API_KEY: {env.get('API_KEY')}") # Still "sk-123"
print(f"DEBUG: {env.get('DEBUG')}") # Now "false"
sandbox.kill()
Update Multiple Variables
Update multiple variables at once:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Set initial configuration
sandbox.env.set_all({
"API_KEY": "sk-123",
"DATABASE_URL": "postgres://localhost/db"
})
# Update multiple variables
sandbox.env.update({
"NODE_ENV": "production",
"DEBUG": "false",
"LOG_LEVEL": "info"
})
# Verify all variables
env = sandbox.env.get_all()
print("All environment variables:")
for key in ["API_KEY", "DATABASE_URL", "NODE_ENV", "DEBUG", "LOG_LEVEL"]:
print(f" {key}: {env.get(key, 'not set')}")
sandbox.kill()
Incremental Configuration
Build environment configuration incrementally:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Step 1: Set base configuration
sandbox.env.set_all({
"NODE_ENV": "production"
})
# Step 2: Add API configuration
sandbox.env.update({
"API_KEY": "sk-prod-xyz",
"API_URL": "https://api.example.com"
})
# Step 3: Add database configuration
sandbox.env.update({
"DATABASE_URL": "postgres://localhost/prod",
"DB_POOL_SIZE": "10"
})
# Step 4: Add logging configuration
sandbox.env.update({
"LOG_LEVEL": "info",
"LOG_FORMAT": "json"
})
# Verify final configuration
env = sandbox.env.get_all()
print(f"Final configuration: {len(env)} variables")
for key in sorted(env.keys()):
if key not in ['PATH', 'HOME', 'USER', 'SHELL']: # Skip system vars
print(f" {key}: {env[key]}")
sandbox.kill()
Override Existing Variables
Update existing variables with new values:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Set initial values
sandbox.env.set_all({
"API_KEY": "sk-dev-123",
"DEBUG": "true",
"LOG_LEVEL": "debug"
})
# Override with production values
sandbox.env.update({
"API_KEY": "sk-prod-xyz",
"DEBUG": "false",
"LOG_LEVEL": "info"
})
# Verify overrides
env = sandbox.env.get_all()
print(f"API_KEY: {env.get('API_KEY')}") # "sk-prod-xyz"
print(f"DEBUG: {env.get('DEBUG')}") # "false"
print(f"LOG_LEVEL: {env.get('LOG_LEVEL')}") # "info"
sandbox.kill()
Complete Example
Here’s a complete example showing incremental environment setup:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Start with base configuration
print("🔧 Setting up environment incrementally...\n")
# Step 1: Base environment
sandbox.env.set_all({"NODE_ENV": "production"})
print("✅ Step 1: Base environment set")
# Step 2: API configuration
sandbox.env.update({
"API_KEY": "sk-prod-xyz",
"API_URL": "https://api.example.com",
"API_TIMEOUT": "30"
})
print("✅ Step 2: API configuration added")
# Step 3: Database configuration
sandbox.env.update({
"DATABASE_URL": "postgres://localhost/prod",
"DB_POOL_SIZE": "10",
"DB_TIMEOUT": "5"
})
print("✅ Step 3: Database configuration added")
# Step 4: Feature flags
sandbox.env.update({
"FEATURE_NEW_UI": "true",
"FEATURE_ANALYTICS": "true",
"FEATURE_BETA": "false"
})
print("✅ Step 4: Feature flags added")
# Step 5: Update existing variable
sandbox.env.update({"NODE_ENV": "staging"})
print("✅ Step 5: NODE_ENV updated to staging")
# Final verification
env = sandbox.env.get_all()
custom_vars = {k: v for k, v in env.items()
if k not in ['PATH', 'HOME', 'USER', 'SHELL', 'PWD']}
print(f"\n📋 Final environment ({len(custom_vars)} custom variables):")
for key, value in sorted(custom_vars.items()):
if any(sensitive in key.upper() for sensitive in ['KEY', 'SECRET', 'PASSWORD']):
print(f" {key}=***MASKED***")
else:
print(f" {key}={value}")
sandbox.kill()
Best Practices
1. Use update() Instead of set_all()
Prefer update() over set_all() to preserve existing variables unless you want to replace everything.
2. Build Incrementally
Build environment configuration incrementally by updating variables in logical groups.
3. Override When Needed
Use update() to override existing variables with new values without affecting others.
4. Group Related Variables
Update related variables together (e.g., all API config, all database config) for better organization.
5. Verify After Updates
Always verify variables are updated correctly using get_all() after updates.
Next Steps