如何在源代码中使用package_data中的数据?

时间:2022-05-26 16:23:39

In setup.py, I have specified package_data like this:

在setup.py中,我指定了package_data,如下所示:

packages=['hermes'],
package_dir={'hermes': 'hermes'},
package_data={'hermes': ['templates/*.tpl']},

And my directory structure is roughly

我的目录结构粗略

hermes/
 |
 | docs/
 | ...
 | hermes/
    | 
    | __init__.py
    | code.py
    | templates
        |
        | python.tpl
 |
 | README
 | setup.py

The problem is that I need to use files from the templates directory in my source code so I can write out python code (this project is a parser generator). I can't seem to figure out how to properly include and use these files from my code. Any ideas?

问题是我需要在源代码中使用templates目录中的文件,这样我就可以写出python代码(这个项目是一个解析器生成器)。我似乎无法弄清楚如何正确地包含和使用我的代码中的这些文件。有任何想法吗?

1 个解决方案

#1


35  

The standard pkgutil module's get_data() function will calculate the path to your data, relative to your package, and retrieve the data for you via whatever module loader Python used to import the hermes package:

标准的pkgutil模块的get_data()函数将计算相对于包的数据路径,并通过Python用于导入hermes包的任何模块加载器为您检索数据:

import pkgutil
data = pkgutil.get_data('hermes', 'templates/python.tpl')

Of course in certain cases you could just read your data using a path calculated from hermes.__file__, but if you plan to distribute your project, consider that it may be installed in different ways on the end user's machine: as plain files, deployed in a zipped egg archive, etc. In the latter case, your hermes module will have been imported by Python using a zipimporter, preventing you from doing a normal open(path).read():

当然,在某些情况下,您可以使用从hermes .__ file__计算的路径来读取数据,但是如果您计划分发项目,请考虑在最终用户的计算机上以不同的方式安装它:作为普通文件,部署在一个压缩的egg存档等。在后一种情况下,你的hermes模块将由Python使用zipimporter导入,阻止你进行正常的打开(路径).read():

>>> import hermes
>>> hermes.__loader__
<zipimporter object "/home/pat/.cascade/virt/foo/lib/python2.6/site-packages/foo-0.0.0-py2.6.egg">

If you're okay with adding a runtime dependency on the distribute codebase, you may want to consdider looking at the pkg_resources module, which can give you the same result but adds other capabilities.

如果您可以在分发代码库上添加运行时依赖项,那么您可能需要查看pkg_resources模块,它可以为您提供相同的结果,但添加了其他功能。

import pkg_resources
data = pkg_resources.resource_string('hermes', 'templates/python.tpl')

#1


35  

The standard pkgutil module's get_data() function will calculate the path to your data, relative to your package, and retrieve the data for you via whatever module loader Python used to import the hermes package:

标准的pkgutil模块的get_data()函数将计算相对于包的数据路径,并通过Python用于导入hermes包的任何模块加载器为您检索数据:

import pkgutil
data = pkgutil.get_data('hermes', 'templates/python.tpl')

Of course in certain cases you could just read your data using a path calculated from hermes.__file__, but if you plan to distribute your project, consider that it may be installed in different ways on the end user's machine: as plain files, deployed in a zipped egg archive, etc. In the latter case, your hermes module will have been imported by Python using a zipimporter, preventing you from doing a normal open(path).read():

当然,在某些情况下,您可以使用从hermes .__ file__计算的路径来读取数据,但是如果您计划分发项目,请考虑在最终用户的计算机上以不同的方式安装它:作为普通文件,部署在一个压缩的egg存档等。在后一种情况下,你的hermes模块将由Python使用zipimporter导入,阻止你进行正常的打开(路径).read():

>>> import hermes
>>> hermes.__loader__
<zipimporter object "/home/pat/.cascade/virt/foo/lib/python2.6/site-packages/foo-0.0.0-py2.6.egg">

If you're okay with adding a runtime dependency on the distribute codebase, you may want to consdider looking at the pkg_resources module, which can give you the same result but adds other capabilities.

如果您可以在分发代码库上添加运行时依赖项,那么您可能需要查看pkg_resources模块,它可以为您提供相同的结果,但添加了其他功能。

import pkg_resources
data = pkg_resources.resource_string('hermes', 'templates/python.tpl')