Django + Celery:ImportError:没有名为project.urls的模块



当我使用以下命令使用 Django 和 Celery 启动一个工作线程时(我在d目录中执行命令(:

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

我正在使用 Django 1.11 和 Celery 4.2 和 Python 2.7。

下面是我的项目结构,其中包含一些对调试有帮助的文件的内容。

姜戈结构:

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')

我的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)

如问题评论中所述,我看到您在导入语句中使用了e.project.celerye.project.settings,但错误说明了找不到project.urls(开始时没有e.(。

您是如何定义ROOT_URLCONF设置的?也许这是导致问题的原因。

相关内容

  • 没有找到相关文章