Python进口和芹菜工人



我有以下目录结构:

.
└── package
├── foo
│   ├── __init__.py
│   └── foo.py
├── run.py
└── tasks.py

foo/foo.py

class Foo:
@staticmethod
def double(x):
return 2 * x

run.py

from package.foo.foo import Foo
def do_bar(x):
return x + 10
if __name__ == '__main__':
foo = Foo()
r1 = foo.double(10)
r2 = do_bar(5)
print(r1 + r2)

tasks.py

from celery import Celery
from package.foo.foo import Foo
from package.run import do_bar
celapp = Celery('foo', broker='amqp://guest@localhost//')

@celapp.task
def run():
foo = Foo()
foo.double(10) + do_bar(5)

当我用$ celery -A tasks worker运行芹菜时,它会给我:

Error: 
Unable to load celery application.
The module package was not found.

如果我只在tasks.py中这样做

from .foo.foo import Foo
from .run import do_bar

我得到ImportError: attempted relative import with no known parent package

我认为我的导入是错误的,但我无法理解Python是如何处理导入的。

正如@Mr_and_Mrs_D在评论中所说:

尝试从包的父目录运行$ celery -A package.tasks worker-python脚本不应该从包内部运行

为我创造了奇迹!

相关内容

  • 没有找到相关文章

最新更新