Accessing data files before and after distutils/setuptools

和自甴很熟 提交于 2019-12-03 11:46:25

I've used a utility method called data_file:

def data_file(fname):
    """Return the path to a data file of ours."""
    return os.path.join(os.path.split(__file__)[0], fname)

I put this in the init.py file in my project, and then call it from anywhere in my package to get a file relative to the package.

Setuptools offers a similar function, but this doesn't need setuptools.

jfs

You could try pkg_resources:

my_data = pkg_resources.resource_string(__name__, fname)
Sebastian Noack

You should use the pkgutil/pkg_resources module to load the data files. It even works from within ziped eggs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!