pytest'no模块命名...&quot问题
因此,当我直接调用测试模块时,我会遇到问题。 Pytest实际上运行良好,但我遇到了试图直接调用我正在测试的Python文件的问题。这是基本文件夹布局:
generate
├── generate
│ ├── __init__.py
│ ├── write_sql.py
│ └── utils.py
└── tests
└── test_write_sql.py
在test_write_sql.py
和write_sql.py
中,我正在使用generate
so so:
from generate import utils
运行pytest
来自root 生成
文件夹正常工作。但是,当我尝试直接从root generate
文件夹中调用write_sql.py
时,我会遇到以下错误:
root@637572f508b9:~/generate# python3 generate/write_sql.py
Traceback (most recent call last):
File "/root/generate/generate/write_sql.py", line 5, in <module>
from generate import utils
ModuleNotFoundError: No module named 'generate'
我的印象是从生成 root文件夹应将
write_sql.py ,然后将直接调用到生成
子文件夹添加为系统路径的模块(由于随附的__ init __. py
file)。我知道是否将导入更改为导入UTILS
write_sql.py.py
有效,但随后pytest运行到导入中错误。
关于我应该使用什么进口方案来获得Pytest和直接调用工作的任何想法?
So I'm running into issues when I run pytest versus when I invoke the tested module directly. Pytest actually runs fine but I run into issues trying to directly invoke the python file that I'm testing. Here is the basic folder layout:
generate
├── generate
│ ├── __init__.py
│ ├── write_sql.py
│ └── utils.py
└── tests
└── test_write_sql.py
Within both test_write_sql.py
and write_sql.py
I'm importing things using the generate
module like so:
from generate import utils
Running pytest
from the root generate
folder works fine. But when I try to invoke write_sql.py
directly from the root generate
folder I run into the following error:
root@637572f508b9:~/generate# python3 generate/write_sql.py
Traceback (most recent call last):
File "/root/generate/generate/write_sql.py", line 5, in <module>
from generate import utils
ModuleNotFoundError: No module named 'generate'
My impression was that running Python from the generate
root folder should add the generate
subfolder as a module to the system path (due to the included __init__.py
file). I know if I change the import to import utils
inside write_sql.py
then the direct call to write_sql.py
works but then pytest runs into import errors.
Any ideas as to what import scheme I should use to get both pytest and the direct invocation working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对导入遇到了同样的问题,我确定的方式正在设置所有导入,以便代码运行。为了运行测试,而不仅仅是调用
pytest
(这运行了它们所在的文件夹的所有测试,因此会有导入错误),应该从类似的根目录中调用:<代码> python3 -m pytest
或
python -m pytest
这样,它从根目录运行所有测试,并且不应给出导入错误
I was having the same issues with imports, the way i fixed it was setting all the imports so that the code runs. In order to run the tests instead of just calling
pytest
(this runs all tests from the folder where they are located, therefore there will be import error) it should be called from the root directory like this:python3 -m pytest
or
python -m pytest
this way it runs all tests from the root directory, and it should not give import errors