Load environment variables from .env, then ensure all required variables are set. If any are missing, raise a RuntimeError with a helpful message pointing to .env.example.
(env_vars: list[str])
| 7 | from dotenv import load_dotenv |
| 8 | |
| 9 | def validate_env_vars(env_vars: list[str]) -> None: |
| 10 | """ |
| 11 | Load environment variables from .env, then ensure all required variables are set. |
| 12 | If any are missing, raise a RuntimeError with a helpful message pointing to .env.example. |
| 13 | """ |
| 14 | # Load from .env into environment |
| 15 | load_dotenv(override=True) |
| 16 | |
| 17 | # Check which vars are missing |
| 18 | missing = [var for var in env_vars if os.getenv(var) is None] |
| 19 | if missing: |
| 20 | raise RuntimeError( |
| 21 | "Missing required environment variables: " |
| 22 | + ", ".join(missing) |
| 23 | + "\n\nPlease create a .env file in the project root " |
| 24 | + "based on .env.example and fill in the values:\n\n" |
| 25 | + "\n".join(f" {var}=" for var in missing) |
| 26 | ) |