Environment variables#

This section explains how to properly access environment variables in a SAF-based solution application.

Warning

Do not call load_dotenv() in your solution code. SAF already loads the .env file at startup and injects the variables into the process environment.

Why load_dotenv() must not be used#

A common but incorrect pattern observed in solution code is:

import os
from dotenv import load_dotenv

load_dotenv()

MY_VARIABLE = os.getenv("MY_VARIABLE")

This pattern appears to work during development (saf run) because the current working directory is the solution root, where the .env file resides. However, in production (installed or deployed solution), the working directory differs and the .env file is not found at the expected path. As a result, load_dotenv() silently does nothing and all variables resolve to None.

SAF handles .env loading for you — the framework reads the file at startup and populates the process environment before your solution code executes. Manually calling load_dotenv() is therefore redundant in development and broken in production.

Summary#

Pattern

Development (saf run)

Production (installed/deployed)

load_dotenv() + os.getenv()

✅ Works

❌ Fails silently

os.getenv() only

✅ Works

✅ Works

Pydantic Settings

✅ Works

✅ Works