使用setuptools/distribute打包资源 [英] Packaging resources with setuptools/distribute

查看:112
本文介绍了使用setuptools/distribute打包资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有多个.txt依赖项的Python鸡蛋(它们是用于由鸡蛋本身生成文件的模板),并且我正努力将这些依赖项在setup.py install期间复制到site-packages.根据 distribute文档 ...

I'm developing an Python egg that has several .txt dependencies (they're templates used to generate files by the egg itself), and I'm struggling to get those dependencies copied to site-packages during setup.py install. According to the distribute documentation...

我的软件包的文件系统:

setup.py
package
|--- __init__.py
|--- main.py
|--- binary (calls main.py with pkg_resources.load_entry_point)
|--- templates
     |--file1.txt
     |--file2.txt

在setup.py中:

setup(
    [...]
    eager_resources = ['templates/file1.txt', 'templates/file2.txt']
)

在我的包裹中:

from pkg_resources import resource_string
tpl = resource_string(__name__, 'templates/file1.txt')

...配置和文件系统的这种组合应导致file1.txtfile2.txt通过pkg_resources.resource_string可用.不幸的是,在setup.py install期间没有将它们复制到site-packages.我想念什么?

...this combination of configuration and filesystem should result in file1.txt and file2.txt being available through pkg_resources.resource_string. Unfortunately, they're not being copied to site-packages during setup.py install. What am I missing?

谢谢!

推荐答案

您是否查看过setuptools文档,其中包含软件包数据:

Have you looked at the setuptools documentation for including package data here: https://setuptools.readthedocs.io/en/latest/setuptools.html#including-data-files

基本上,您只需要在setup.py文件中设置include_package_data=True.如果您使用的是Subversion或CVS,则将包含所有版本化的文件.如果没有,则可以指定MANIFEST.in文件中要包含的文件.

Basically, you just need to set include_package_data=True in your setup.py file. If you are using subversion or CVS, all versioned files will be included. If not, you can specify which files to include with a MANIFEST.in file.

我相信分发也支持这一点.

I believe distribute supports this as well.

然后,您可以像打包文件一样访问文件.即在main.py中,您可以:

You can then access the files as you would without them being packaged. i.e. in main.py you could have:

import os.path
f = open(os.path.join(os.path.dirname(__file__),'templates','file1.txt'))
print f.read()
f.close()

,这在打包版本中也适用.一个警告是,您还必须在setup.py中设置zip_safe = False,以便在安装过程中解压缩所有文件.

and this would work in the packaged version as well. One caveat is that you will have to also set zip_safe = False in setup.py so that all the files are unzipped during installation.

这篇关于使用setuptools/distribute打包资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆