当我使用celery worker
命令启动芹菜工作时,我收到以下错误。否则,Django项目似乎运行正常。
$ celery -A testproj worker -l info
Traceback (most recent call last):
File "/Users/devang/virtualenvs/testproj/bin/celery", line 11, in <module>
sys.exit(main())
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/__main__.py", line 30, in main
main()
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/bin/celery.py", line 81, in main
cmd.execute_from_commandline(argv)
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/bin/celery.py", line 769, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/bin/base.py", line 306, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/bin/celery.py", line 761, in handle_argv
return self.execute(command, argv)
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/bin/celery.py", line 693, in execute
).run_from_argv(self.prog_name, argv[1:], command=argv[0])
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/bin/worker.py", line 179, in run_from_argv
return self(*args, **options)
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/bin/base.py", line 269, in __call__
ret = self.run(*args, **kwargs)
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/bin/worker.py", line 212, in run
state_db=self.node_format(state_db, hostname), **kwargs
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/worker/__init__.py", line 95, in __init__
self.app.loader.init_worker()
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/loaders/base.py", line 128, in init_worker
self.import_default_modules()
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/loaders/base.py", line 116, in import_default_modules
signals.import_modules.send(sender=self.app)
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/utils/dispatch/signal.py", line 166, in send
response = receiver(signal=self, sender=sender, **named)
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/amqp/utils.py", line 42, in __call__
self.set_error_state(exc)
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/amqp/utils.py", line 39, in __call__
**dict(self.kwargs, **kwargs) if self.kwargs else kwargs
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/app/base.py", line 327, in _autodiscover_tasks
self.loader.autodiscover_tasks(packages, related_name)
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/loaders/base.py", line 251, in autodiscover_tasks
related_name) if mod)
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/loaders/base.py", line 272, in autodiscover_tasks
return [find_related_module(pkg, related_name) for pkg in packages]
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/celery/loaders/base.py", line 282, in find_related_module
pkg_path = importlib.import_module(package).__path__
File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/cacheops/__init__.py", line 8, in <module>
install_cacheops()
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/cacheops/query.py", line 571, in install_cacheops
for model in get_models(include_auto_created=True):
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/django/db/models/loading.py", line 232, in get_models
self._populate()
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/django/db/models/loading.py", line 75, in _populate
self.load_app(app_name, True)
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/django/db/models/loading.py", line 97, in load_app
app_module = import_module(app_name)
File "/Users/devang/virtualenvs/testproj/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
ImportError: No module named accounts
我安装的应用看起来像 - INSTALLED_APPS =( &#39; django.contrib.admin&#39 ;, &#39; django.contrib.auth&#39 ;, &#39; django.contrib.contenttypes&#39 ;, &#39; django.contrib.sessions&#39 ;, &#39; django.contrib.sites&#39 ;, &#39; django.contrib.messages&#39 ;, &#39; django.contrib.staticfiles&#39 ;, &#39; cacheops&#39 ;, &#39;帐户&#39 ;, )
我已根据链接http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#django-first-steps
中的建议在Django中安装了芹菜项目答案 0 :(得分:2)
发现问题。我的Django项目结构没有遵循 -
的默认结构
--proj
|-- __init__.py
|-- settings.py
--accounts
|--__init__.py
--app2
|-- __init__.py
相反,如果遵循:
--proj
|-- __init__.py
|-- settings.py
|-- apps
|--accounts
|--__init__.py
|--app2
|-- __init__.py
导致Celery找到该模块。
答案 1 :(得分:0)
此外,如果你有这样的结构,你可以这样“破解”它:
sys.path.append(os.path.abspath('apps'))
设置前
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
它对我有用。当然,您必须将sys.path
中的路径调整为项目结构。