{"id":"7536e665f3a028b9","repo":"pallets/flask","slug":"the-environment-variable-variable-name-r-is-not","errorCode":null,"errorMessage":"The environment variable {variable_name!r} is not set and as such configuration could not be loaded. Set this variable and make it point to a configuration file","messagePattern":"The environment variable (.+?) is not set and as such configuration could not be loaded\\. Set this variable and make it point to a configuration file","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/flask/config.py","lineNumber":118,"sourceCode":"        self.root_path = root_path\n\n    def from_envvar(self, variable_name: str, silent: bool = False) -> bool:\n        \"\"\"Loads a configuration from an environment variable pointing to\n        a configuration file.  This is basically just a shortcut with nicer\n        error messages for this line of code::\n\n            app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])\n\n        :param variable_name: name of the environment variable\n        :param silent: set to ``True`` if you want silent failure for missing\n                       files.\n        :return: ``True`` if the file was loaded successfully.\n        \"\"\"\n        rv = os.environ.get(variable_name)\n        if not rv:\n            if silent:\n                return False\n            raise RuntimeError(\n                f\"The environment variable {variable_name!r} is not set\"\n                \" and as such configuration could not be loaded. Set\"\n                \" this variable and make it point to a configuration\"\n                \" file\"\n            )\n        return self.from_pyfile(rv, silent=silent)\n\n    def from_prefixed_env(\n        self, prefix: str = \"FLASK\", *, loads: t.Callable[[str], t.Any] = json.loads\n    ) -> bool:\n        \"\"\"Load any environment variables that start with ``FLASK_``,\n        dropping the prefix from the env key for the config key. Values\n        are passed through a loading function to attempt to convert them\n        to more specific types than strings.\n\n        Keys are loaded in :func:`sorted` order.\n\n        The default loading function attempts to parse values as any","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/config.py#L100-L136","documentation":"`Config.from_envvar(variable_name)` is a shortcut for `from_pyfile(os.environ[variable_name])` (src/flask/config.py:102-124). If the named environment variable is unset or empty and `silent=False` (the default), Flask raises this RuntimeError instead of proceeding without configuration. It exists so deployments fail loudly when the pointer to the config file is missing, rather than running with defaults.","triggerScenarios":"Calling `app.config.from_envvar('YOURAPP_SETTINGS')` when that env var is not exported in the process environment, or is set to an empty string. A set-but-wrong path instead produces a file-not-found error from `from_pyfile`, not this one.","commonSituations":"Env var exported in an interactive shell but not in the service's environment (systemd, Docker, cron, supervisord); var set without `export` so child processes don't inherit it; typo or case mismatch in the variable name; WSGI servers (uWSGI/gunicorn under systemd) started with a scrubbed environment; CI runs missing the secret/vars block.","solutions":["Export the variable pointing at your config file before starting the app: `export YOURAPP_SETTINGS=/etc/yourapp/settings.cfg`.","Verify the name matches exactly (env var names are case-sensitive) and that it is set in the actual runtime environment (Dockerfile `ENV`, compose `environment:`, systemd `Environment=`), not just your shell.","If the config file is optional, call `app.config.from_envvar('YOURAPP_SETTINGS', silent=True)` and handle the False return.","Consider `app.config.from_prefixed_env()` if you want config values directly from FLASK_* env vars instead of a pointer file."],"exampleFix":"# before (shell)\npython run.py  # YOURAPP_SETTINGS never set -> RuntimeError\n\n# after (shell)\nexport YOURAPP_SETTINGS=/etc/yourapp/production.cfg\npython run.py\n\n# or in code, if optional:\napp.config.from_envvar('YOURAPP_SETTINGS', silent=True)","handlingStrategy":"validation","validationCode":"import os\n\nCONFIG_VAR = \"MYAPP_SETTINGS\"\nif not os.environ.get(CONFIG_VAR):\n    raise SystemExit(f\"{CONFIG_VAR} is not set; export it to point at your config file\")\napp.config.from_envvar(CONFIG_VAR)","typeGuard":"import os\n\ndef config_envvar_ready(name: str) -> bool:\n    path = os.environ.get(name)\n    return bool(path) and os.path.isfile(path)","tryCatchPattern":"try:\n    app.config.from_envvar(\"MYAPP_SETTINGS\")\nexcept RuntimeError as exc:\n    raise SystemExit(f\"Configuration error: {exc}\") from exc","preventionTips":["Check `os.environ` for the variable at startup and exit with a clear message before calling `config.from_envvar()`.","Document required environment variables in the README and provide a `.env.example` (names only, no values).","Only use `from_envvar(..., silent=True)` when a missing config file is genuinely optional — silent fallbacks hide misconfiguration.","Verify the variable points to an existing, readable file, not just that it is set."],"tags":["flask","python","configuration","environment-variables"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}