When I start a worker using Django and Celery using this command (I execute the command within the d
directory):
当我使用这个命令使用Django和Celery启动一个worker时(我在d目录中执行命令):
celery -A e.project.celery:app worker -l info
celery -A e.project.celery:app worker -l info
File "/Users/.../..../e/venv/lib/python2.7/site-packages/celery-4.2.0-py2.7.egg/celery/loaders/base.py", line 108, in import_default_modules
raise response
ImportError: No module named project.urls
I am using Django 1.11 and Celery 4.2 with Python 2.7.
我在Python 2.7中使用Django 1.11和Celery 4.2。
Below is my project structure with the contents of some files that should be helpful for debugging.
下面是我的项目结构,其中包含一些有助于调试的文件的内容。
Django structure:
d/
__init__.py
e/
__init__.py
core/
__init__.py
app1/
__init__.py
tasks.py
project/ (project stuff)
__init__.py
celery.py
settings.py
urls.py
context_processors.py
wsgi.py
manage.py
setup.py
settings.py
:
INSTALLED_APPS = [
'...',
'django_celery_results',
'django_celery_beat'
]
CELERY_RESULT_BACKEND = 'django-db'
CELERY_BROKER_URL = 'redis://foo:foo@127.0.0.1:6599/1'
CELERY_CELERYBEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
e/project/__init__.py
:
from __future__ import absolute_import, unicode_literals
from e.project.celery import app as celery_app
__all__ = ('celery_app', )
e/project/celery.py
:
from __future__ import absolute_import, unicode_literals
import os
import sys
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "e.project.settings")
app = Celery('e_tasks')
# Using a string here means the worker doesn'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:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
if __name__ == '__main__':
app.start()
d/e/core/app1/tasks.py
:
from celery import shared_task
@shared_task
def hello_world_task():
print('Hello world')
My manage.py
file:
我的manage.py文件:
#!/usr/local/bin/python2.7
import os
import sys
if __name__ == "__main__":
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'e'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "e.project.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
1 个解决方案
#1
3
As stated in the comments to the question, I see that you use e.project.celery
and e.project.settings
in your import statements, but the error says something about not finding project.urls
(there is no e.
at the start).
正如对问题的评论中所述,我看到你在import语句中使用了e.project.celery和e.project.settings,但错误说明了没有找到project.urls(开头没有e。) )。
How did you define the ROOT_URLCONF
setting? Maybe that is causing the problem.
你是如何定义ROOT_URLCONF设置的?也许这就是导致问题的原因。
#1
3
As stated in the comments to the question, I see that you use e.project.celery
and e.project.settings
in your import statements, but the error says something about not finding project.urls
(there is no e.
at the start).
正如对问题的评论中所述,我看到你在import语句中使用了e.project.celery和e.project.settings,但错误说明了没有找到project.urls(开头没有e。) )。
How did you define the ROOT_URLCONF
setting? Maybe that is causing the problem.
你是如何定义ROOT_URLCONF设置的?也许这就是导致问题的原因。