芹菜';模块';对象没有属性';app&x27;使用Python3时
我正在学习芹菜教程。他们正在使用Python2,我正在尝试使用python3实现同样的功能 我有两个文件: 芹菜项目芹菜';模块';对象没有属性';app&x27;使用Python3时,python,python-3.x,celery,Python,Python 3.x,Celery,我正在学习芹菜教程。他们正在使用Python2,我正在尝试使用python3实现同样的功能 我有两个文件: 芹菜项目 from celery import Celery app = Celery( 'proj', broker='amqp://', backend='amqp://', include=['proj.tasks']) app.conf.update(Celery_TAST_RESULT_EXPIRES=3600,) if __name__ == '__main__'
from celery import Celery
app = Celery(
'proj', broker='amqp://', backend='amqp://', include=['proj.tasks'])
app.conf.update(Celery_TAST_RESULT_EXPIRES=3600,)
if __name__ == '__main__':
app.start()
from celery_proj import app
@app.task
def add(x, y):
return x + y
@app.task
def mul(x, y):
return x * y
@app.task
def xsum(numbers):
return sum(numbers)
和任务。py:
from celery import Celery
app = Celery(
'proj', broker='amqp://', backend='amqp://', include=['proj.tasks'])
app.conf.update(Celery_TAST_RESULT_EXPIRES=3600,)
if __name__ == '__main__':
app.start()
from celery_proj import app
@app.task
def add(x, y):
return x + y
@app.task
def mul(x, y):
return x * y
@app.task
def xsum(numbers):
return sum(numbers)
当我尝试运行芹菜-A项目工人-l信息时,我得到:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/celery/app/utils.py", line 235, in find_app
found = sym.app
AttributeError: 'module' object has no attribute 'app'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/bin/celery", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python3.4/dist-packages/celery/__main__.py", line 30, in main
main()
File "/usr/local/lib/python3.4/dist-packages/celery/bin/celery.py", line 81, in main
cmd.execute_from_commandline(argv)
File "/usr/local/lib/python3.4/dist-packages/celery/bin/celery.py", line 769, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/usr/local/lib/python3.4/dist-packages/celery/bin/base.py", line 309, in execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "/usr/local/lib/python3.4/dist-packages/celery/bin/base.py", line 469, in setup_app_from_commandline
self.app = self.find_app(app)
File "/usr/local/lib/python3.4/dist-packages/celery/bin/base.py", line 489, in find_app
return find_app(app, symbol_by_name=self.symbol_by_name)
File "/usr/local/lib/python3.4/dist-packages/celery/app/utils.py", line 240, in find_app
found = sym.celery
AttributeError: 'module' object has no attribute 'celery'
回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python3.4/dist packages/芹菜/app/utils.py”,第235行,在find_应用程序中
found=sym.app
AttributeError:“模块”对象没有属性“应用”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/usr/local/bin/芹菜”,第11行,在
sys.exit(main())
文件“/usr/local/lib/python3.4/dist-packages/cellery/_-main__.py”,第30行,在main中
main()
文件“/usr/local/lib/python3.4/dist packages/celery/bin/celery.py”,第81行,在main中
cmd.从命令行(argv)执行命令
文件“/usr/local/lib/python3.4/dist packages/celery/bin/celery.py”,第769行,从命令行执行
超级(CeleryCommand,self)。从命令行(argv)执行命令)
文件“/usr/local/lib/python3.4/dist-packages/芹菜/bin/base.py”,第309行,从命令行执行
argv=self.setup\u app\u from\u命令行(argv)
文件“/usr/local/lib/python3.4/dist-packages/芹菜/bin/base.py”,第469行,在安装程序中
self.app=self.find_应用程序(app)
文件“/usr/local/lib/python3.4/dist packages/芹菜/bin/base.py”,第489行,在find_应用程序中
返回find\u app(app,symbol\u by\u name=self.symbol\u by\u name)
文件“/usr/local/lib/python3.4/dist packages/芹菜/app/utils.py”,第240行,在find_应用程序中
找到的=西芹
AttributeError:“模块”对象没有属性“芹菜”
我做错了什么?如何解决这个问题?我一直在玩游戏,了解一件有趣的事情。 当我们运行
芹菜-A项目工人-l信息时,我们基本上运行proj
文件夹。我相信,当您运行proj
文件夹时,芹菜会在那里查找芹菜.py
文件。在python2中还可以,因为我们使用了绝对导入,并且可以从proj.芹菜导入中写入。
,但在python3中没有这种可能性。我们需要从芹菜导入中写入。
,这将导致错误,因为芹菜
是一个模块,因此我们需要将芹菜.py重命名为其他名称。当我们这样做时,我们不能再运行proj
。也许我错了,但至少我做到了
您需要做的是打开proj
目录并从那里运行tasks.py,只有这样您才能使用芹菜项目导入应用程序中的并保留芹菜项目
如果我弄错了,请写信并添加您的解决方案。如果您使用的是Python 3,您可以通过以下方式使用绝对导入:from\uuuuuuu future\uuuuu导入绝对\u导入
下面是我最近的Python3 Django 1.7应用程序中的一个示例芹菜.py
:
from __future__ import absolute_import
import os
import django
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_app.settings')
django.setup()
app = Celery('my_app')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
当您使用芹菜运行芹菜时-项目工人
AttributeError: 'module' object has no attribute 'app'
...
AttributeError: 'module' object has no attribute 'celery'
告诉您它试图找到:
a) 应用程序
实例
b) 项目芹菜
模块
因此,您可以将从芹菜项目导入应用程序添加到到中,或者将您的芹菜项目py
重命名为芹菜项目py
或者,您可以将芹菜作为芹菜-项目芹菜\u项目工人运行
AttributeError: 'module' object has no attribute 'app'
...
AttributeError: 'module' object has no attribute 'celery'
我找到了答案,因为stackoverflow上没有答案对我有帮助。在docker容器中运行芹菜和django应用程序时,我也遇到了同样的问题。看起来芹菜工作者命令在当前目录中搜索芹菜应用程序。从任意目录运行该命令时,会发生“未找到属性”错误。但是,当它从django应用程序目录(在那里可以找到应用程序/模块)运行时,芹菜工人按预期运行。当我为主项目目录和应用程序目录提供相同的名称时,我遇到了docker的一个奇怪情况
更正卷解决问题中的路径(./app:/app)检查docker部分中的芹菜配置部分
-app:
..
app:
__init__.py
celery.py
init.py
from __future__ import absolute_import
from .celery import app as celery_app
__all__ = ['celery_app']
芹菜
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
app = Celery('app')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
因此,我必须在卷部分提供正确的路径
celery:
build: .
command: celery -A app worker -l info
volumes:
- ./app:/app
depends_on:
- db
- redis