Repository: celery/celery
File: celery/bin/celery.py (Python)

Code before:
              envvar='CONFIG_MODULE',
              cls=CeleryOption,
              help_group="Global Options")
@click.option('--workdir',
              cls=CeleryOption,
              type=pathlib.Path,
              callback=lambda _, __, wd: os.chdir(wd) if wd else None,
              is_eager=True,
              help_group="Global Options")
@click.option('-C',
              '--no-color',
              envvar='NO_COLOR',
              is_flag=True,
              cls=CeleryOption,
              help_group="Global Options")
@click.option('-q',
              '--quiet',
              is_flag=True,
              cls=CeleryOption,
              help_group="Global Options")
@click.option('--version',
              cls=CeleryOption,
              is_flag=True,
              help_group="Global Options")
@click.option('--skip-checks',
              envvar='SKIP_CHECKS',
              cls=CeleryOption,
              is_flag=True,
              help_group="Global Options",
              help="Skip Django core checks on startup. Setting the SKIP_CHECKS environment "
                   "variable to any non-empty string will have the same effect.")
@click.pass_context
def celery(ctx, app, broker, result_backend, loader, config, workdir,
           no_color, quiet, version, skip_checks):
    """Celery command entrypoint."""
    if version:
        click.echo(VERSION_BANNER)
        ctx.exit()
    elif ctx.invoked_subcommand is None:
        click.echo(ctx.get_help())
        ctx.exit()

    if loader:
        # Default app takes loader from this env (Issue #1066).
        os.environ['CELERY_LOADER'] = loader
    if broker:
        os.environ['CELERY_BROKER_URL'] = broker
    if result_backend:
        os.environ['CELERY_RESULT_BACKEND'] = result_backend
    if config:
        os.environ['CELERY_CONFIG_MODULE'] = config

Review comment (bug):
This line crashes in our celery application from `5.5.0rc2`. As our application worked with `rc1` I'm wondering if this is a bug (i.e. worth opening an issue) or some mistake in our startup procedure. What do you think @Nusnus? 

```
celery_app = Celery("modulename") 
...
celery_app.worker_main(argv)
# calls celery_app.start() > celery.main() > celery(...) > find_app(app)

Traceback (most recent call last):
  File ".../lib/python3.12/site-packages/celery/bin/celery.py", line 135, in celery
    app_object = find_app(app)
                 ^^^^^^^^^^^^^
  File ".../lib/python3.12/site-packages/celery/app/utils.py", line 383, in find_app
    sym = symbol_by_name(app, imp=imp)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.12/site-packages/kombu/utils/imports.py", line 59, in symbol_by_name
    module = imp(module_name, package=package, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.12/site-packages/celery/utils/imports.py", line 109, in import_from_cwd
    return imp(module, package=package)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.7_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
ModuleNotFoundError: No module named '<Celery modulename at 0x...>'
```


Code after:
                   "variable to any non-empty string will have the same effect.")
@click.pass_context
def celery(ctx, app, broker, result_backend, loader, config, workdir,
           no_color, quiet, version, skip_checks):
    """Celery command entrypoint."""
    if version:
        click.echo(VERSION_BANNER)
        ctx.exit()
    elif ctx.invoked_subcommand is None:
        click.echo(ctx.get_help())
        ctx.exit()

    if loader:
        # Default app takes loader from this env (Issue #1066).
        os.environ['CELERY_LOADER'] = loader
    if broker:
        os.environ['CELERY_BROKER_URL'] = broker
    if result_backend:
        os.environ['CELERY_RESULT_BACKEND'] = result_backend
    if config:
        os.environ['CELERY_CONFIG_MODULE'] = config
    if skip_checks:
        os.environ['CELERY_SKIP_CHECKS'] = 'true'

    try:
        app_object = find_app(app)
    except ModuleNotFoundError as e:
        if e.name != app:
            exc = traceback.format_exc()
            ctx.fail(
                UNABLE_TO_LOAD_APP_ERROR_OCCURRED.format(app, exc)
            )
        ctx.fail(UNABLE_TO_LOAD_APP_MODULE_NOT_FOUND.format(e.name))
    except AttributeError as e:
        attribute_name = e.args[0].capitalize()
        ctx.fail(UNABLE_TO_LOAD_APP_APP_MISSING.format(attribute_name))
    except Exception:
        exc = traceback.format_exc()
        ctx.fail(
            UNABLE_TO_LOAD_APP_ERROR_OCCURRED.format(app, exc)
        )

    ctx.obj = CLIContext(app=app_object, no_color=no_color, workdir=workdir,
                         quiet=quiet)

    # User options
    worker.params.extend(ctx.obj.app.user_options.get('worker', []))
    beat.params.extend(ctx.obj.app.user_options.get('beat', []))
    events.params.extend(ctx.obj.app.user_options.get('events', []))

    for command in celery.commands.values():