pytest'no模块命名...&quot问题

发布于 2025-02-06 01:48:36 字数 1143 浏览 3 评论 0原文

因此,当我直接调用测试模块时,我会遇到问题。 Pytest实际上运行良好,但我遇到了试图直接调用我正在测试的Python文件的问题。这是基本文件夹布局:

generate
├── generate
│   ├── __init__.py
│   ├── write_sql.py
│   └── utils.py
└── tests
    └── test_write_sql.py

test_write_sql.pywrite_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文件夹应将生成子文件夹添加为系统路径的模块(由于随附的__ init __. py file)。我知道是否将导入更改为导入UTILS write_sql.py ,然后将直接调用到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清欢 2025-02-13 01:48:36

我对导入遇到了同样的问题,我确定的方式正在设置所有导入,以便代码运行。为了运行测试,而不仅仅是调用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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文