“Can’t run pytest with tmpdir: ‘AttributeError: module ‘py’ has no attribute ‘path'”

huangapple go评论110阅读模式
英文:

Can't run pytest with tmpdir: "AttributeError: module 'py' has no attribute 'path'"

问题

我通过pip install pytest安装了pytest,然后运行了以下简单的测试:

  1. def test_sf_bayer_training(tmpdir):
  2. print(tmpdir)
  3. _config = TestingTrainerConfigInstance()
  4. _config.name = 'test_sf_bayer_training'
  5. _config.output_path = tmpdir / _config.name
  6. _curr_p = os.getcwd()
  7. _config.model_config_path = '.models/sf.yaml'
  8. print(_config)
  9. return

我收到了以下错误:

  1. File "/home/davids/venvs/vai/lib/python3.10/site-packages/_pytest/_io/terminalwriter.py", line 10, in <module>
  2. from _pytest.compat import final
  3. File "/home/davids/venvs/vai/lib/python3.10/site-packages/_pytest/compat.py", line 44, in <module>
  4. LEGACY_PATH = py.path.local
  5. AttributeError: module 'py' has no attribute 'path'
  6. Process finished with exit code 1

当我在本地检查时:

  1. (vai) ~ ipython
  2. Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]
  3. Type 'copyright', 'credits' or 'license' for more information
  4. IPython 8.8.0 -- An enhanced Interactive Python. Type '?' for help.
  5. In [1]: import py
  6. In [2]: py.path.local
  7. Out[2]: py._path.local.LocalPath

我应该如何解决pytest的问题以便能够运行?

我的pytest包版本:

  1. py==1.11.0
  2. pytest==7.4.0
英文:

I installed pytest via pip install pytest then ran the following simple test:

  1. def test_sf_bayer_training(tmpdir):
  2. print(tmpdir)
  3. _config = TestingTrainerConfigInstance()
  4. _config.name = &#39;test_sf_bayer_training&#39;
  5. _config.output_path = tmpdir / _config.name
  6. _curr_p = os.getcwd()
  7. _config.model_config_path = f&#39;.models/sf.yaml&#39;
  8. print(_config)
  9. return

I received the following error:

  1. File &quot;/home/davids/venvs/vai/lib/python3.10/site-packages/_pytest/_io/terminalwriter.py&quot;, line 10, in &lt;module&gt;
  2. from _pytest.compat import final
  3. File &quot;/home/davids/venvs/vai/lib/python3.10/site-packages/_pytest/compat.py&quot;, line 44, in &lt;module&gt;
  4. LEGACY_PATH = py.path. local
  5. AttributeError: module &#39;py&#39; has no attribute &#39;path&#39;
  6. Process finished with exit code 1

When I check locally:

  1. (vai) ~ ipython
  2. Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]
  3. Type &#39;copyright&#39;, &#39;credits&#39; or &#39;license&#39; for more information
  4. IPython 8.8.0 -- An enhanced Interactive Python. Type &#39;?&#39; for help.
  5. In [1]: import py
  6. In [2]: py.path. local
  7. Out[2]: py._path.local.LocalPath

How can I solve the issue with pytest so it can run?

My pytest package:

  1. py==1.11.0
  2. pytest==7.4.0

答案1

得分: 2

问题是显然有一个py模块覆盖了pytest的内部使用,导致了错误。

我通过重新创建我的环境,满足最低要求以激活pytest来解决了这个问题。

英文:

The problem was that apperantly there was a py module that overshadowed the interal usage of pytest which caused the error.

I solved it by recreating my environment with the minimal requirments to activate pytest.

答案2

得分: 1

请使用 tmp_path fixture 代替 tmpdir,它返回一个标准库 pathlib.Path 实例。tmpdir fixture 已经过时。

你问题中展示的基本用法是相同的,所以你可以在这段代码中将 tmpdir 替换为 tmp_path

英文:

You're on pytest 7.x, please use tmp_path fixture instead, which returns a stdlib pathlib.Path instance. The tmpdir fixture is legacy.

The basic usage shown in your question is the same, so you can just find/replace tmpdir with tmp_path in this code.

答案3

得分: 0

因为我安装了 pytest 7.x,但 pytest-benchmark 仍然是 3.4.x 版本,所以出现了这个错误。将 pytest-benchmark 升级到 4.0.0 版本解决了问题。

英文:

I got this error because I had pytest 7.x installed, but pytest-benchmark was still at 3.4.x. Upgrading pytest-benchmark to 4.0.0 fixed the issue.

huangapple
  • 本文由 发表于 2023年7月6日 13:31:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定