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

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

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

问题

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

def test_sf_bayer_training(tmpdir):
    print(tmpdir)
    _config = TestingTrainerConfigInstance()
    _config.name = 'test_sf_bayer_training'
    _config.output_path = tmpdir / _config.name
    _curr_p = os.getcwd()
    _config.model_config_path = '.models/sf.yaml'
    print(_config)
    return

我收到了以下错误:

  File "/home/davids/venvs/vai/lib/python3.10/site-packages/_pytest/_io/terminalwriter.py", line 10, in <module>
    from _pytest.compat import final
  File "/home/davids/venvs/vai/lib/python3.10/site-packages/_pytest/compat.py", line 44, in <module>
    LEGACY_PATH = py.path.local
AttributeError: module 'py' has no attribute 'path'

Process finished with exit code 1

当我在本地检查时:

(vai)   ~ ipython
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.8.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import py

In [2]: py.path.local
Out[2]: py._path.local.LocalPath

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

我的pytest包版本:

py==1.11.0
pytest==7.4.0
英文:

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

def test_sf_bayer_training(tmpdir):
    print(tmpdir)
    _config = TestingTrainerConfigInstance()
    _config.name = &#39;test_sf_bayer_training&#39;
    _config.output_path = tmpdir / _config.name
    _curr_p = os.getcwd()
    _config.model_config_path = f&#39;.models/sf.yaml&#39;
    print(_config)
    return

I received the following error:

  File &quot;/home/davids/venvs/vai/lib/python3.10/site-packages/_pytest/_io/terminalwriter.py&quot;, line 10, in &lt;module&gt;
    from _pytest.compat import final
  File &quot;/home/davids/venvs/vai/lib/python3.10/site-packages/_pytest/compat.py&quot;, line 44, in &lt;module&gt;
    LEGACY_PATH = py.path. local
AttributeError: module &#39;py&#39; has no attribute &#39;path&#39;

Process finished with exit code 1

When I check locally:

(vai) ➜  ~ ipython
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]
Type &#39;copyright&#39;, &#39;credits&#39; or &#39;license&#39; for more information
IPython 8.8.0 -- An enhanced Interactive Python. Type &#39;?&#39; for help.

In [1]: import py

In [2]: py.path. local
Out[2]: py._path.local.LocalPath

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

My pytest package:

py==1.11.0
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:

确定