python attribute_error ai_generated true

属性错误:'TempPathFactory' 对象没有属性 'mktemp'

AttributeError: 'TempPathFactory' object has no attribute 'mktemp'

ID: python/pytest-tmpdir-factory-error

其他格式: JSON · Markdown 中文 · English
80%修复率
83%置信度
0证据数
2025-06-15首次发现

版本兼容性

版本状态引入弃用备注
7.4.0 active

根因分析

在非 pytest 环境中使用了 tmp_path_factory,或版本不支持该方法。

English

在非 pytest 环境中使用了 tmp_path_factory,或版本不支持该方法。

generic

解决方案

  1. 100% 成功率
    在测试函数中直接使用 `tmp_path` 参数,pytest 自动提供。
  2. 85% 成功率
    确保 pytest >= 7.0 并使用 `tmp_path_factory.mktemp('name')`。

无效尝试

常见但无效的做法:

  1. 50% 失败

    绕过 pytest 的临时目录管理,可能冲突。

  2. 40% 失败

    如果已是最新版本,无济于事。