使用django配置芹菜:app.config_from_object(' django.conf:settings')不起作用

时间:2017-04-06 20:21:43

标签: django celery django-settings

首先,我在我的项目中使用Python 2.7,Django 1.9.12和Celery 4.0.2 我正在关注" Django"的第一步芹菜教程。
这是我的celeryapp.py:

<project>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <!-- Replace with the version of the MySQL driver you want to use -->
            <version>${mysql-version}</version>
        </dependency>
    </dependencies>
</project>


我可以通过这种方式使其工作,具有全局设置

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
import sys

sys.path.append('vagrant/nested_tree/nested_tree_project')

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'nested_tree_project.settings')

app = Celery('nested_tree_project', broker='amqp://localhost//')

# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:global_settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


而我实际上无法访问我的项目设置。如果我使用

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

当我开始芹菜工作时我得到一个错误

'django.conf:settings'


我究竟做错了什么?我试图从同一目录运行python shell:

Traceback (most recent call last):
  File "/vagrant/nested_tree/venv/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/__main__.py", line 14, in main
    _main()
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/bin/celery.py", line 326, in main
    cmd.execute_from_commandline(argv)
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/bin/celery.py", line 488, in execute_from_com
mandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/bin/base.py", line 281, in execute_from_comma
ndline
    return self.handle_argv(self.prog_name, argv[1:])
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/bin/celery.py", line 480, in handle_argv
    return self.execute(command, argv)
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/bin/celery.py", line 412, in execute
    ).run_from_argv(self.prog_name, argv[1:], command=argv[0])
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/bin/worker.py", line 221, in run_from_argv
    return self(*args, **options)
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/bin/base.py", line 244, in __call__
    ret = self.run(*args, **kwargs)
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/bin/worker.py", line 255, in run
    **kwargs)
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/worker/worker.py", line 94, in __init__
    self.app.loader.init_worker()
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/loaders/base.py", line 116, in init_worker
    self.import_default_modules()
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/loaders/base.py", line 111, in import_default
_modules
    return [self.import_task_module(m) for m in self.default_modules]
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/loaders/base.py", line 97, in import_task_mod
ule
    return self.import_from_cwd(module)
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/loaders/base.py", line 106, in import_from_cw
d
    package=package,
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/utils/imports.py", line 101, in import_from_c
wd
    return imp(module, package=package)
  File "/vagrant/nested_tree/venv/local/lib/python2.7/site-packages/celery/loaders/base.py", line 100, in import_module
    return importlib.import_module(module, package=package)
  File "/usr/lib/python2.7/importlib/__init__.py", line 32, in import_module
    raise TypeError("relative imports require the 'package' argument")

一切正常。

如果它可能有用,这是我的项目层次结构: hierarchy

1 个答案:

答案 0 :(得分:0)

也许Celery安装错了。我已经创建了新的虚拟环境,现在可以正常运行。