【发布时间】:2021-09-27 11:04:15
【问题描述】:
运行pytest 命令的文件夹里面有__init__.py。这就是为什么测试无法从utils 导入模块的原因。此行为符合 docs 中规定的 pytest 要求,即 basedir 不应包含 __init__.py。
有没有办法在包含__init__.py 的目录中运行pytest?
下面是文件夹结构。
.
├── __init__.py
├── tests
└── utils
如果从文件夹中删除__init__.py,则pytest 成功。
【问题讨论】:
-
为什么不从不同的目录运行 pytest?确实,如果您尝试运行一个包,并且该包的基本目录不在您的
sys.path/PYTHONPATH上,那么确实包相对导入将无法正常工作。 -
你建议在哪个目录运行它?由于 pytest 行为,我会偶然发现同样的问题:
determine basedir: this is the first “upward” (towards the root) directory not containing an __init__.py
标签: python python-3.x pytest