GitHub user dosubot[bot] added a comment to the discussion: Multi-Tenant User management
<!-- Answer --> This error happens because Superset loads superset_config.py before the Flask app context is established, so any imports or logic in that file (or modules it imports) that depend on app.config or Flask globals will fail with "Working outside of application context" [reference](https://github.com/apache/superset/blob/308007f909b5a3c1cdd6fc5632ef27ca7eb97ce0/superset/config.py). To fix this, move all imports and logic that depend on the Flask app (like importing RoleRestAPI or registering custom APIs) inside the FLASK_APP_MUTATOR function in your superset_config.py. Only run them inside this function, which is called after the app context is ready [reference](https://github.com/apache/superset/blob/308007f909b5a3c1cdd6fc5632ef27ca7eb97ce0/superset/initialization/__init__.py). For example: ```python # superset_config.py def FLASK_APP_MUTATOR(app): from custom_role_api import TenantRoleRestAPI # Import here, not at top-level # Register or monkey-patch your API here # Example: appbuilder.add_api(TenantRoleRestAPI) ``` Make sure you do not import custom_role_api or RoleRestAPI at the top level of superset_config.py or any module that is imported before the app context exists. This pattern matches how Superset itself registers APIs and avoids context errors [reference](https://github.com/apache/superset/blob/308007f909b5a3c1cdd6fc5632ef27ca7eb97ce0/superset/initialization/__init__.py). If you follow this pattern, your customizations will run safely after the app is initialized, and the error should go away. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/082dc424-620b-4730-b189-189f6d299f1b?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/082dc424-620b-4730-b189-189f6d299f1b?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/082dc424-620b-4730-b189-189f6d299f1b?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/082dc424-620b-4730-b189-189f6d299f1b?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/082dc424-620b-4730-b189-189f6d299f1b?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/082dc424-620b-4730-b189-189f6d299f1b?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/082dc424-620b-4730-b189-189f6d299f1b?feedback_type=other)</sup> [](https://go.dosu.dev/discord-bot) [](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/discussions/33877) GitHub link: https://github.com/apache/superset/discussions/33877#discussioncomment-13637524 ---- This is an automatically sent email for [email protected]. To unsubscribe, please send an email to: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
