pkgutil get\ u data:AttributeError:'NoneType'对象没有属性'decode'

2024-04-20 07:19:10 发布

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

我编写了一个测试,在其中获取本地资源文件,如下所示:

from pkgutil import get_data

@fixture(scope='session')
def ref_o_full():
    return pd.read_csv(StringIO(get_data('test_data', 'ref_o.csv').decode()))

运行测试时,出现以下异常:

test setup failed
@fixture(scope='session')
    def ref_o_full():
>       return pd.read_csv(StringIO(get_data('test_data', 'ref_o.csv').decode('utf8')))
E       AttributeError: 'NoneType' object has no attribute 'decode'

但是,如果在调试模式下运行测试,则不会发生此异常。我觉得这是某种比赛状态?你知道吗

我已改用以下每次都有效的方法:

from pkg_resources import resource_filename
from os.path import join as join_path

@fixture(scope='session')
def ref_o_full():
    dir = resource_filename(__name__, 'test_data')
    return pd.read_csv(join_path(dir, 'ref_o.csv'))

Tags: csvfromtestimportrefreaddataget