我有以下目录结构:
.
└── package
├── foo
│ ├── __init__.py
│ └── foo.py
├── run.py
└── tasks.py
富/富.py
class Foo:
@staticmethod
def double(x):
return 2 * x
运行.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)
任务.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 如何处理导入。