英文:
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 = 'test_sf_bayer_training'
_config.output_path = tmpdir / _config.name
_curr_p = os.getcwd()
_config.model_config_path = f'.models/sf.yaml'
print(_config)
return
I received the following error:
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
When I check locally:
(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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论