AttributeError:“module”对象没有属性“ensuretemp”

2024-10-02 00:31:44 发布

您现在位置:Python中文网/ 问答频道 /正文

遵循http://py.readthedocs.org/en/latest/path.html#basic-interactive-example处的示例

import py
temppath = py.test.ensuretemp('py.path_documentation')

引发错误 AttributeError:“module”对象没有属性“ensuretemp”

Python版本3.4.3,py版本1.4.26。在

^{pr2}$

我做错什么了吗?在


Tags: pathpyorgimport版本http示例basic
2条回答

不,你做的每件事都是正确的。我也不能让它工作。另一方面,pytest有fixture tmpdir,它可以满足您在测试中的需要:为您的测试提供唯一的临时目录。在

下面是一个测试示例:

def test_one(tmpdir):
    test_file = tmpdir.join('dir', 'file').ensure(file=True)
    test_file.write('test\ntest\n')

    assert test_file.read().rsplit() == ['test', 'test']

另一方面,pytest使用py.path路径,它有py.path.local对象,并使用tempfile.mkdtemp创建临时目录对象:

^{pr2}$

您也可以直接从py.path直接使用mkdtemp

In [127]: import py.path    
In [128]: py.path.local.mkdtemp()
Out[128]: local('/tmp/tmpP1835f')

相关问题 更多 >

    热门问题