Python Celery Worker 错误:ImportError 没有名为 celery 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19577172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:05:25  来源:igfitidea点击:

Celery Worker Error: ImportError no module named celery

pythonflaskcelerycelery-task

提问by user2216194

I am getting an import error when I try to start my celery worker. I am not sure what the issue is. Any help would be highly appreciated.

当我尝试启动 celery worker 时出现导入错误。我不确定是什么问题。任何帮助将不胜感激。

My project:

我的项目:

email/__init__.py
    /celery.py

I try to run the application by calling :

我尝试通过调用运行应用程序:

celery worker --app=email

I have followed all the steps here - http://docs.celeryproject.org/en/latest/getting-started/next-steps.html#about-the-app-argument

我已经按照这里的所有步骤 - http://docs.celeryproject.org/en/latest/getting-started/next-steps.html#about-the-app-argument

The traceback:

回溯:

File "/Users/.../bin/celery", line 9, in <module>
    load_entry_point('celery==3.0.24', 'console_scripts', 'celery')()
File "/Users/.../lib/python2.7/site-packages/celery/__main__.py, line 14, in main
main()
File "/Users/.../lib/python2.7/site-packages/celery/bin/celery.py", line 957, in main
cmd.execute_from_commandline(argv)
File "/Users/.../lib/python2.7/site-packages/celery/bin/celery.py", line 901, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/Users/.../lib/python2.7/site-packages/celery/bin/base.py", line 185, in execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "/Users/.../lib/python2.7/site-packages/celery/bin/base.py", line 300, in setup_app_from_commandline
self.app = self.find_app(app)
File "/Users/.../lib/python2.7/site-packages/celery/bin/base.py", line 317, in find_app
return self.find_app('%s.celery:' % (app.replace(':', ''), ))
File "/Users/.../lib/python2.7/site-packages/celery/bin/base.py", line 311, in find_app
sym = self.symbol_by_name(app)
File "/Users/.../lib/python2.7/site-packages/celery/bin/base.py", line 322, in symbol_by_name
return symbol_by_name(name, imp=import_from_cwd)
File "/Users/.../lib/python2.7/site-packages/kombu/utils/__init__.py", line 80, in symbol_by_name
module = imp(module_name, package=package, **kwargs)
File "/Users/.../lib/python2.7/site-packages/celery/utils/imports.py", line 99, in import_from_cwd
return imp(module, package=package)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named celery

Here is my celery.py

这是我的 celery.py

from __future__ import absolute_import

from celery import Celery
from app import mail

celery = Celery('email.celery', 
                broker = 'amqp://guest:guest@localhost:5672//',
                backend = 'amqp://')

if __name__ == '__main__':
    celery.start()

@celery.task
def send_email(nickname, email):
    mail.send(msg) 

采纳答案by Michael W.

The problem is that you're saying "hey bro I heard you liked celery have some celery".

问题是你在说“嘿兄弟,我听说你喜欢芹菜有一些芹菜”。

But really you should be saying, "hey bro I heard you installed celery, lets make a file named something really similar so we don't confuse the hell out of our environment".

但实际上您应该说,“嘿兄弟,我听说您安装了 celery,让我们创建一个名为非常相似的文件,这样我们就不会混淆我们的环境”。

Rename your email/celery.pyfile to email/celery_app.py

将您的email/celery.py文件重命名为email/celery_app.py

Then, when you start your worker, do the following:

然后,当您启动工作程序时,请执行以下操作:

celery -A email worker --app=email.celery_app:app --loglevel=info # etc.

The key is that you need to nothave the file named celery.pyin your file-structure, but if you don't, then you can't rely on celery to find celery, so you have to point it by specifying --app manually.

关键是,你需要具有命名的文件celery.py在你的文件结构,但如果你不这样做,那么你可以不依靠芹菜找到芹菜,所以你必须手动指定--app点吧。

回答by Mark

I was actually having a similar issue, assuming you were following the Next Steps tutorialand all I had to do to resolve the issue was run the worker from the directory above proj, that is, assuming you're currently in the projdirectory run:

我实际上遇到了类似的问题,假设您正在遵循Next Steps 教程,而我解决问题所需要做的就是从proj上面的目录运行工作程序,也就是说,假设您当前在proj目录中运行:

cd ..
celery -A proj worker -l info

It's working for me now.

它现在对我有用。