ModuleNotFoundError: No module named 'django.config' using celery and rabbitmq

ModuleNotFoundError: No module named 'django.config' using celery and rabbitmq

我在 运行 执行以下命令后收到此错误:

C:\Users\callu\project_name>celery -A project_name worker -l info

我想知道这是否与我没有在虚拟环境中创建我的 django 项目有关,但我找不到任何关于这个问题的信息。如果是因为它在虚拟环境之外,我不确定如何在不重新启动项目的情况下解决它(是否容易移动到 PyCharm 中的 venv?)

我 运行 我的 django 服务器和我上面的 celery 命令在这里:

C:\Users\callu\project_name>

我 运行 在另一个位置(不是 venv)启动我的 RabbitMQ 服务器,但我认为这不是问题所在

完整追溯:

Traceback (most recent call last):
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\cached_property.py", line 70, in __get__
    return obj_dict[name]
KeyError: 'data'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\callu\AppData\Local\Programs\Python\Python37-32\Scripts\celery.exe\__main__.py", line 7, in <module>
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\__main__.py", line 15, in main
    sys.exit(_main())
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\bin\celery.py", line 213, in main
    return celery(auto_envvar_prefix="CELERY")
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\click\core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\click\core.py", line 717, in main
    rv = self.invoke(ctx)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\click\core.py", line 1135, in invoke
    sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\click\core.py", line 641, in make_context
    self.parse_args(ctx, args)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\click\core.py", line 940, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\click\core.py", line 1477, in handle_parse_result
    self.callback, ctx, self, value)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\click\core.py", line 96, in invoke_param_callback
    return callback(ctx, param, value)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\bin\worker.py", line 156, in <lambda>
    value: value or ctx.obj.app.conf.worker_state_db,
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\utils\collections.py", line 111, in __getattr__
    return self[k]
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\utils\collections.py", line 391, in __getitem__
    return getitem(k)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\utils\collections.py", line 249, in __getitem__
    return mapping[_key]
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\collections\__init__.py", line 1021, in __getitem__
    if key in self.data:
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\kombu\utils\objects.py", line 29, in __get__
    return super().__get__(instance, owner)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\cached_property.py", line 74, in __get__
    return obj_dict.setdefault(name, self.func(obj))
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\base.py", line 138, in data
    return self.callback()
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\base.py", line 946, in _finalize_pending_conf
    conf = self._conf = self._load_config()
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\base.py", line 956, in _load_config
    self.loader.config_from_object(self._config_source)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\loaders\base.py", line 123, in config_from_object
    obj = self._smart_import(obj, imp=self.import_from_cwd)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\loaders\base.py", line 136, in _smart_import
    return symbol_by_name(path, imp=imp)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\kombu\utils\imports.py", line 56, in symbol_by_name
    module = imp(module_name, package=package, **kwargs)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\loaders\base.py", line 95, in import_from_cwd
    package=package,
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\utils\imports.py", line 100, in import_from_cwd
    return imp(module, package=package)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\site-packages\celery\loaders\base.py", line 89, in import_module
    return importlib.import_module(module, package=package)
  File "c:\users\callu\appdata\local\programs\python\python37-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'django.config'

听起来你在某个地方(在你的应用程序中/celery.py 如果使用标准布局)一行看起来像:

app.config_from_object('django.config:settings', namespace='CELERY')

其中 app 是您的 Celery 应用程序对象。

但是你需要的是:

app.config_from_object('django.conf:settings', namespace='CELERY')

注意:您的 django 配置模块是 django.conf 而不是 django.config