芹菜工人 ImportError:没有名为“项目”的模块
Posted
技术标签:
【中文标题】芹菜工人 ImportError:没有名为“项目”的模块【英文标题】:Celery worker ImportError: No module named 'project' 【发布时间】:2019-02-12 02:28:18 【问题描述】:当我尝试启动 worker 时,我遇到了一个问题:ImportError: No module named 'project'
Traceback(最近一次通话最后一次):
File "/usr/local/bin/celery", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python3.5/dist-packages/celery/__main__.py", line 16, in main
_main()
File "/usr/local/lib/python3.5/dist-packages/celery/bin/celery.py", line 322, in main
cmd.execute_from_commandline(argv)
File "/usr/local/lib/python3.5/dist-packages/celery/bin/celery.py", line 496, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/usr/local/lib/python3.5/dist-packages/celery/bin/base.py", line 273, in execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "/usr/local/lib/python3.5/dist-packages/celery/bin/base.py", line 479, in setup_app_from_commandline
self.app = self.find_app(app)
File "/usr/local/lib/python3.5/dist-packages/celery/bin/base.py", line 501, in find_app
return find_app(app, symbol_by_name=self.symbol_by_name)
File "/usr/local/lib/python3.5/dist-packages/celery/app/utils.py", line 359, in find_app
sym = symbol_by_name(app, imp=imp)
File "/usr/local/lib/python3.5/dist-packages/celery/bin/base.py", line 504, in symbol_by_name
return imports.symbol_by_name(name, imp=imp)
File "/usr/local/lib/python3.5/dist-packages/kombu/utils/imports.py", line 56, in symbol_by_name
module = imp(module_name, package=package, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/celery/utils/imports.py", line 104, in import_from_cwd
return imp(module, package=package)
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 956, in _find_and_load_unlocked
ImportError: No module named 'project'
我在 ubuntu16.04 中使用 celery 和 redis。以下是我的处理步骤:
先安装celery和redis:
pip install -U celery[redis]
sudo apt-get install redis-server
当我使用时:
ps aux|grep redis
我可以看到redis列出了6379端口
第二个在project.setting中添加:
#Broker
BROKER_URL = 'redis://127.0.0.1:6379/0'
BROKER_TRANSPORT = 'redis'
第三次构建项目文件夹下的celery.py,project.setting也在这个文件夹中:
from __future__ import absolute_import
import os
import django
from celery import Celery
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
django.setup()
app = Celery('project')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
在将使用它的应用程序下构建tasks.py
from project.celery import app
from random import Random
from django.core.mail import send_mail
from users.models import EmailVerifyRecord
from project.settings import EMAIL_FROM
@app.task
def random_str(randomlength=8):
str = ''
chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
length = len(chars) - 1
random = Random()
for i in range(randomlength):
str += chars[random.randint(0, length)]
return str
在所有这些步骤之后,我在 celery.py 的同一路径上运行:
celery -A project worker -l debug
然后我遇到了问题。
有朋友可以帮忙吗?
非常感谢!
【问题讨论】:
确保你在你的 env 项目中,并且在同一个目录中 【参考方案1】:这只是因为你不在你的根项目中
导航到您的根项目,与 manage.py
相同的目录
cd path/to/project
然后再次运行命令。
celery -A project worker -l debug
【讨论】:
你真的很摇滚! 你能帮忙解决这个问题吗***.com/questions/52229388/…非常感谢! 你好朋友能不能帮我解答一下这个问题***.com/questions/52399006/…我已经花了2天时间解决了,但还是没有非常感谢!以上是关于芹菜工人 ImportError:没有名为“项目”的模块的主要内容,如果未能解决你的问题,请参考以下文章