Python 路径错误ImportError:尝试在没有已知父包的情况下进行相对导入
项目如下Python 路径错误ImportError:尝试在没有已知父包的情况下进行相对导入,python,celery,Python,Celery,项目如下 server ├── celery.py ├── tasks.py tasks.py from __future__ import absolute_import, unicode_literals from sys import path from .celery import app_celery from time import sleep @app_celery.task def my_func(): sleep(5) print("Process fi
server
├── celery.py
├── tasks.py
tasks.py
from __future__ import absolute_import, unicode_literals
from sys import path
from .celery import app_celery
from time import sleep
@app_celery.task
def my_func():
sleep(5)
print("Process finished")
芹菜
from __future__ import absolute_import, unicode_literals
from celery import Celery
app_celery = Celery('server',
broker='redis://',
backend='redis://',
include=['server.tasks'])
app_celery.conf.update(
result_expires=3600,
)
if __name__ == '__main__':
app_celery.start()
芹菜-服务器工作人员-l信息
启动良好,进行如下测试,非常完美
$ ipython
Python 3.7.4 (default, Aug 13 2019, 20:35:49)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.8.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from server.tasks import my_func
In [2]: my_func.delay()
Out[2]: <AsyncResult: 39a21329-0e80-4f6e-ac08-536832414756>
当我在server
下运行python test.py
时,它会报告
Traceback (most recent call last):
File "test.py", line 2, in <module>
from tasks import my_func
File "/xxx/server/tasks.py", line 3, in <module>
from .celery import app_celery
ImportError: attempted relative import with no known parent package
回溯(最近一次呼叫最后一次):
文件“test.py”,第2行,在
从任务导入我的功能
文件“/xxx/server/tasks.py”,第3行,在
来自。芹菜导入应用程序\芹菜
ImportError:尝试在没有已知父包的情况下进行相对导入
在服务器目录中添加一个\uuuu init\uuuuu.py
文件。此文件可以为空
Python只允许在常规包中进行相对导入。常规包定义为包含\uuuu init\uuuuu.py
文件的目录
感谢您的帮助,它仍然报告相同的情况error@ComplicatedPhenomenon您键入的文件名正确吗?两端都有两个下划线。我再次检查,结果是。@complexPhenomenon尝试将
芹菜.py
重命名为其他名称。它与实际芹菜包装冲突,可能是问题的原因之一。当我将Celly.py
更改为Celly_1.py
并进行其他相关更改时,错误保持不变。(from.cellery\u 1导入应用程序\u cellery ImportError:尝试在没有已知父包的情况下进行相对导入
)
Traceback (most recent call last):
File "test.py", line 2, in <module>
from tasks import my_func
File "/xxx/server/tasks.py", line 3, in <module>
from .celery import app_celery
ImportError: attempted relative import with no known parent package