Configuration
pyetm uses environment variables and configuration files to manage settings for API access, SSL certificates, and other options.
Configuration Methods
pyetm supports multiple ways to configure your environment:
- Environment file (
.env) - Recommended for local development - Environment variables - Useful for CI/CD and production deployments
- Programmatic configuration - For advanced use cases
Environment File (.env)
The easiest way to configure pyetm is with a .env file in your project root. The pyetm init command will create this for you:
Example .env File
# ETM API Authentication
ETM_API_TOKEN=etm_your.token.here
# Environment Selection
ENVIRONMENT=pro
# Error Handling (optional)
PYETM_ERROR_MODE=default
# SSL Configuration (optional)
SSL_VERIFY=true
# SSL_CERT_PATH=/path/to/custom/ca-bundle.crt
# CSV Settings (optional)
CSV_SEPARATOR=,
DECIMAL_SEPARATOR=.
Configuration Options
ETM_API_TOKEN
Your personal ETM API token for authentication.
- Required for: Saving scenarios, accessing private scenarios
- Optional for: Creating and working with public scenarios
- Format: Must start with
etm_prefix - Get your token: ETM API Authentication Docs
Security
Never commit your .env file to version control! Add it to .gitignore.
ENVIRONMENT
Specifies which ETM environment to connect to.
Options:
pro(default) - Production environment (energytransitionmodel.com)beta- Beta testing environmentlocal- Local development serverYYYY-MM- Stable release tag (e.g.,2024-12)
Example:
BASE_URL
Custom API base URL (advanced users only).
- Default: Inferred from
ENVIRONMENTsetting - Use case: Custom or self-hosted ETM instances
Example:
PYETM_ERROR_MODE
Controls how pyetm handles errors and warnings during operations.
Options:
safe- All warnings raise exceptions (maximum safety)default(default) - Smart behavior: errors raise in single operations, collected in bulkdangerous- No warnings raise, all logged only
When to use each mode:
default mode (recommended for most users):
Single operations raise on errors immediately:
Bulk operations collect warnings and continue:
scenarios = Scenarios.create_many([...]) # Continues processing
scenarios.show_warnings() # Display collected warnings
safe mode (recommended for CI/production):
All warnings raise exceptions, even in bulk operations. Use this when you want maximum safety and can't tolerate any partial failures.
dangerous mode (for exploratory data analysis):
Nothing raises exceptions - all warnings are logged. Useful when you want to process as much data as possible and handle errors afterwards.
Relationship with LOG_LEVEL
PYETM_ERROR_MODE and LOG_LEVEL are independent:
PYETM_ERROR_MODEcontrols operational flow (raise exceptions vs. collect warnings)LOG_LEVELcontrols system logging verbosity (what gets logged by Python's logger)
User-facing warnings from show_warnings() are always displayed regardless of LOG_LEVEL.
Example:
LOG_LEVEL
Controls logging verbosity for system/debug messages. This is an advanced option for debugging.
Options: DEBUG, INFO (default), WARNING, ERROR, CRITICAL
Example:
Advanced Users Only
Most users don't need to change LOG_LEVEL. It controls internal logging, not user-facing warnings. Use PYETM_ERROR_MODE to control error handling behavior instead.
SSL Configuration
SSL_VERIFY
Enable or disable SSL certificate verification.
- Default:
true - Set to
falseonly for: Testing with self-signed certificates in development
Example:
SSL_CERT_PATH
Path to a custom CA certificate bundle for SSL verification.
- Use case: Corporate networks with custom CA certificates
- Format: Path to a
.crtor.pemfile
Example:
TRUST_ENV
Trust system environment proxy settings.
- Default:
false - Reads:
HTTP_PROXY,HTTPS_PROXY,NO_PROXYenvironment variables
Example:
CSV Settings
CSV_SEPARATOR
Character used to separate CSV columns.
- Default:
,(comma) - Common alternatives:
;(semicolon) for European formats
Example:
DECIMAL_SEPARATOR
Character used for decimal points in numbers.
- Default:
.(period) - Common alternatives:
,(comma) for European formats
Example:
Programmatic Configuration
For advanced use cases, you can configure pyetm programmatically:
from pyetm import Client
from pyetm.config import AppConfig
# Create custom configuration
config = AppConfig(
etm_api_token="etm_your.token.here",
environment="pro",
error_mode="default", # or "safe" or "dangerous"
log_level="DEBUG",
ssl_verify=True,
)
# Initialize client with custom config
client = Client(config=config)
Runtime Configuration Changes
For testing or interactive workflows, you may need to reload configuration from environment variables after they've been modified at runtime.
reload_configuration()
Use the reload_configuration() function to reload all settings from environment variables:
import os
from pyetm.config import reload_configuration
# Modify environment variable
os.environ["PYETM_ERROR_MODE"] = "dangerous"
# Reload configuration to pick up changes
reload_configuration()
# Settings now reflect the new environment
This function clears both the settings cache and the error policy cache, ensuring that the next operation will use the updated configuration.
When to use:
- Testing: Switch between error modes or environments during test runs
- Interactive notebooks: Dynamically adjust configuration based on analysis needs
- Runtime adjustments: Change settings without restarting your Python session
Important notes:
Production Use
In production code, configuration should typically be set once at startup via environment variables or .env files. Runtime configuration changes modify global state and should be used cautiously.
Alternative: Programmatic Configuration
For most use cases, prefer programmatic configuration (see above) over runtime reloading. This provides better control and avoids global state modifications.
Example: Testing with different error modes
import os
from pyetm.config import reload_configuration
from pyetm import Scenarios
# Test with default mode
scenarios = Scenarios.create_many([...]) # Collects warnings
# Switch to safe mode for next test
os.environ["PYETM_ERROR_MODE"] = "safe"
reload_configuration()
# Now warnings will raise exceptions
scenarios = Scenarios.create_many([...]) # Raises on first warning
Environment-Specific Setup
Development Environment
ETM_API_TOKEN=etm_dev.token.here
ENVIRONMENT=beta
PYETM_ERROR_MODE=default
LOG_LEVEL=DEBUG
SSL_VERIFY=true
Production Environment
ETM_API_TOKEN=etm_prod.token.here
ENVIRONMENT=pro
PYETM_ERROR_MODE=safe
LOG_LEVEL=WARNING
SSL_VERIFY=true
TRUST_ENV=true
Data Analysis / Exploratory
Local Testing
ETM_API_TOKEN=etm_local.token.here
ENVIRONMENT=local
PYETM_ERROR_MODE=default
LOG_LEVEL=DEBUG
SSL_VERIFY=false
BASE_URL=http://localhost:3000/api/v3
SSL Certificate Issues
If you encounter SSL certificate errors, try these solutions:
1. Update certifi
2. Use Custom CA Bundle
For corporate networks with custom CAs:
3. Disable SSL Verification (Development Only)
4. System Certificates
On macOS with corporate proxies:
# Export system certificates
security find-certificate -a -p /System/Library/Keychains/SystemRootCertificates.keychain > /tmp/ca-bundle.crt
# Configure pyetm
Validation
pyetm validates your configuration on startup. If you have issues, you can test your config:
from pyetm.config import AppConfig
try:
config = AppConfig()
print("Configuration valid!")
print(f"Environment: {config.environment}")
print(f"Base URL: {config.base_url}")
except Exception as e:
print(f"Configuration error: {e}")
Configuration Priority
When multiple configuration sources exist, pyetm uses this priority order:
- Programmatic configuration (highest priority)
- Environment variables
.envfile- Default values (lowest priority)
Best Practices
- Use
.envfiles for local development - Use environment variables for CI/CD and production
- Never commit tokens or
.envfiles to version control - Use different tokens for different environments
- Keep SSL verification enabled in production
- Use
safeerror mode in CI/production environments - Use
defaulterror mode for most development work - Use
dangerousmode only for exploratory data analysis
Next Steps
- Quick Start Guide - Create your first scenario
- Working with Scenarios - Learn about scenario management
- API Reference - Complete API documentation