首页 > 解决方案 > 子目录中的 __init__.py 文件是否会触发父目录中的 __init__.py 文件?(Python 3.9)

问题描述

我的 Python 项目目前正遇到我的依赖项存在冲突的问题。function_a.py在我为代码( in file_a.py)中的特定函数编写完测试之后,我想稍后解决这个问题。

当我在此目录 ( ) 中的测试文件上显式运行 pytest 时,pytest test_a.py一切都很好,我的测试通过了。但是,如果我将__init__.py文件添加到此目录,则由于依赖项之间的冲突(__init__.py在父目录的文件中导入),我的代码会出错。

作品:

- parent_directory
 - other_files.py
 - __init__.py
 - child_directory
  - file_a.py
  - test_a.py
  

不工作:

- parent_directory
 - other_files.py
 - __init__.py
 - child_directory
  - file_a.py
  - test_a.py
  - __init__.py

为什么__init__.pychild_directory 中的 会导致__init__.pyparent_directory 中的我的 other 被执行?

标签: pythonpython-3.x

解决方案


推荐阅读