def set_settings(ctx, instance=None):
    """Pick correct settings instance and set it to a global variable."""
    global settings

    settings = None

    _echo_enabled = ctx.invoked_subcommand not in [
        "get",
        "inspect",
        "debug-info",
        None,
    ]
    if "--json" in sys.argv:
        _echo_enabled = False

    if instance is not None:
        if ctx.invoked_subcommand in ["init"]:
            raise click.UsageError(
                "-i/--instance option is not allowed for `init` command"
            )
        sys.path.insert(0, ".")
        settings = import_settings(instance)
    elif "FLASK_APP" in os.environ:  # pragma: no cover
        with suppress(ImportError, click.UsageError):
            from flask.cli import ScriptInfo  # noqa
            from dynaconf import FlaskDynaconf

            app_import_path = os.environ["FLASK_APP"]
            flask_app = ScriptInfo(app_import_path).load_app()
            settings = FlaskDynaconf(flask_app, **flask_app.config).settings
            if _echo_enabled:
                click.echo(
                    click.style(
                        "Flask app detected", fg="white", bg="bright_black"
                    )
                )
    elif "DJANGO_SETTINGS_MODULE" in os.environ:  # pragma: no cover
        sys.path.insert(0, os.path.abspath(os.getcwd()))
        import django  # noqa

        django.setup()  # ensure django is setup to avoid AppRegistryNotReady
        settings_module = import__django_settings(
            os.environ["DJANGO_SETTINGS_MODULE"]
        )
        found_settings = python_inspect.getmembers(
            settings_module,
            lambda item: isinstance(item, (LazySettings, Settings)),
        )
        if found_settings:
            settings = found_settings[0][1]
        if settings is not None and _echo_enabled:
            click.echo(
                click.style(
                    "Django app detected", fg="white", bg="bright_black"
                )
            )

    if settings is None:
        if instance is None and "--help" not in sys.argv:
            if ctx.invoked_subcommand and ctx.invoked_subcommand not in [
                "init",
            ]:
                warnings.warn(
                    "Starting on 3.x the param --instance/-i is now required. "
                    "try passing it `dynaconf -i path.to.settings <cmd>` "
                    "Example `dynaconf -i config.settings list` "
                )
                settings = legacy_settings
            else:
                settings = LazySettings(create_new_settings=True)
        else:
            settings = LazySettings()
Pick correct settings instance and set it to a global variable.
dynaconf/cli.py