如何在Python setup.py中递归添加包数据? [英] How to add package data recursively in Python setup.py?
问题描述
我有一个新的库,其中必须包含许多小数据文件的子文件夹,并且我正在尝试将它们添加为包数据。想象我的图书馆是这样的:
I have a new library that has to include a lot of subfolders of small datafiles, and I'm trying to add them as package data. Imagine I have my library as so:
library
- foo.py
- bar.py
data
subfolderA
subfolderA1
subfolderA2
subfolderB
subfolderB1
...
我想通过setup.py在所有子文件夹中添加所有数据,但是似乎我必须手动进入每个子文件夹(有100个等等),然后添加一个 init .py文件。此外,setup.py会递归找到这些文件,还是我需要在setup.py中手动添加所有这些文件,例如:
I want to add all of the data in all of the subfolders through setup.py, but it seems like I manually have to go into every single subfolder (there are 100 or so) and add an init.py file. Furthermore, will setup.py find these files recursively, or do I need to manually add all of these in setup.py like:
package_data={
'mypackage.data.folderA': ['*'],
'mypackage.data.folderA.subfolderA1': ['*'],
'mypackage.data.folderA.subfolderA2': ['*']
},
我可以执行此操作与剧本,但似乎超级痛苦。
I can do this with a script, but seems like a super pain. How can I achieve this in setup.py?
PS,这些文件夹的层次结构很重要,因为这是一个包含实质文件的数据库,我们希望文件树成为在将它们通过GUI呈现给用户时保留下来,因此使该文件结构保持完整是我们的优势。
PS, the hierarchy of these folders is important because this is a database of material files and we want the file tree to be preserved when we present them in a GUI to the user, so it would be to our advantage to keep this file structure intact.
推荐答案
- 使用 Setuptools 代替distutils。
- 使用数据文件代替打包数据。这些不需要
__ init __。py
。 -
使用标准Python代码生成文件和目录列表,而无需编写它的字面意思是:
- Use Setuptools instead of distutils.
- Use data files instead of package data. These do not require
__init__.py
. Generate the lists of files and directories using standard Python code, instead of writing it literally:
data_files = []
directories = glob.glob('data/subfolder?/subfolder??/')
for directory in directories:
files = glob.glob(directory+'*')
data_files.append((directory, files))
# then pass data_files to setup()
这篇关于如何在Python setup.py中递归添加包数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!