我一直在 Celery: First steps with django導入錯誤:沒有模塊中凸出/凸出/ celery.py名爲 '芹菜'
凸出/凸出/ celery.py教程如下:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
app = Celery('proj')
# 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:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
PROJ/PROJ/INIT的.py:
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ['celery_app']
sampleapp/tasks.py:
# Create your tasks here
from __future__ import absolute_import, unicode_literals
from celery import shared_task
@shared_task
def add(x, y):
return x + y
@shared_task
def mul(x, y):
return x * y
@shared_task
def xsum(numbers):
return sum(numbers)
我敢肯定我正確地遵循它,但是我得到一個錯誤信息,當我開始運行python3 manage.py runserver
這是錯誤消息:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 316, in execute
settings.INSTALLED_APPS
File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 97, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/app/main/actinbox/__init__.py", line 5, in <module>
from .celery import app as celery_app
File "/app/main/actinbox/celery.py", line 3, in <module>
from celery import Celery
ImportError: No module named 'celery'
我已經安裝了芹菜,以及作爲rabbitmq-server。
你們有什麼想法發生了什麼? 我錯過了什麼嗎?
我芹菜的版本是4.0.0 的Django 1.10.2 蟒蛇3.5.2
你有'芹菜'安裝?如果沒有,請執行'sudo pip3 install芹菜'('pip3',而不是'pip',因爲您使用的是python3) –
哦謝謝..我會嘗試 –
不要使用名稱'celery.py'作爲您的文件,因爲現在'導入'加載你的文件,而不是預期的庫。 – furas