【发布时间】:2016-01-26 16:17:21
【问题描述】:
小上下文:我正在用 Python 编写一个使用 Celery 的项目。我使用了一个通用的实用函数框架(我自己编写的),将 pip 作为本地模块安装,实用程序库也用于其他项目。
我有一个这样的项目目录:
project/
__init__.py
celery.py
test_runner.py
some_more_files_that_dont_matter_for_this_example.py
还有一个我作为 python 模块安装的实用程序项目,如下所示:
__init__.py
utils/
__init__.py
utils.py
setup.py
现在,我有一些代码可以在 celery.py 中初始化 celery,其中包括 from utils.utils import Utils。 test_runner.py 看起来像这样:
from utils.utils import Utils
if __name__ == '__main__':
print 1+1
我的utils.py 有以下导入:from celery import Celery 应该指的是我使用pip install celery 安装的 celery 模块。
现在当我执行python test_runner.py 时出现问题的是它尝试导入utils.py,然后显然导入project/celery.py(不是我pip 安装的芹菜模块),这使得循环依赖发生(因为@ 987654333@ 也导入utils/utils.py)。
有人可以向我解释如何确保utils/utils.py 中的from celery import Celery 仅导入实际安装的pip celery 模块,更重要的是:为什么导入会这样?我正在运行 Python 3.5,我认为如果我使用 from . import x,它只会执行这些本地导入。
【问题讨论】:
标签: python python-3.x import