英文:
Add a data directory outside Python package directory
问题
以下是已翻译的内容:
给定包my_package
的以下目录结构:
/
├── data/
│ ├── more_data/
│ └── foo.txt
├── my_package/
│ ├── __init__.py
│ └── stuff/
│ └── __init__.py
├── README.md
├── setup.cfg
├── setup.py
如何在代码内以最Pythonic的方式让data/
目录可访问,而不使用__file__
或其他hacky解决方案?我已经尝试在setup.py
中使用data_files
和在setup.cfg
中使用[options.package_data]
,但都没有成功。
我想要做类似以下的操作:
dir_data = importlib.resources.files(data)
csv_files = dir_data.glob('*.csv')
编辑:
我正在使用可编辑的安装,并且包内已经有一个data/
目录(与顶层数据无关的源代码)。
英文:
Given the following directory structure for a package my_package
:
/
├── data/
│ ├── more_data/
│ └── foo.txt
├── my_package/
│ ├── __init__.py
│ └── stuff/
│ └── __init__.py
├── README.md
├── setup.cfg
├── setup.py
How can I make the data/
directory accessible (in the most Pythonic way) from within code, without using __file__
or other hacky solutions? I have tried using data_files
in setup.py
and the [options.package_data]
in setup.cfg
to no avail.
I would like to do something like:
dir_data = importlib.resources.files(data)
csv_files = dir_data.glob('*.csv')
EDIT:
I'm working with an editable installation and there's already a data/
directory in the package (for source code unrelated to the top-level data).
答案1
得分: 1
我能想到两种解决方案。对于这两种解决方案,目标是将数据移动到顶层可导入的包中(这就是为什么它被称为“包数据”)。这意味着可以通过类似以下方式访问数据:
importlib.resource.files('mypackage.data')
1. 在源代码仓库中移动文件
一个简单而直接的解决方案是将 data/
目录移动到 my_package/data/
。然后像往常一样进行打包。
2. 让 setuptools 在构建时“移动”数据
通过 setuptools 的 package_dir
特性,可以在构建时修改目录结构。
请注意,这种方法不适用于“可编辑安装”。
在 setup.cfg
中可能如下(未经测试):
[options]
# ...
package_dir=
my_package=my_package
my_package.data=data
英文:
I can think of 2 solutions. For both solutions, the object is to move data into the top-level importable package (that is why it is called "package data"). This means the data will be accessible via something like the following:
importlib.resource.files('mypackage.data')
1. Move files in the source code repository
One easy and straightforward solution is to move the data/
directory to my_package/data/
. And then do the packaging as usual.
2. Let setuptools "move" the data at build-time
With the package_dir
feature of setuptools, it is possible to modify the directory structure at build time.
Be aware that this approach does not work with "editable installations".
It could look like this in setup.cfg
(untested):
[options]
# ...
package_dir=
my_package=my_package
my_package.data=data
答案2
得分: 1
创建一个空的 data/__init__.py
文件,以使 data
成为一个顶层导入包,从而数据文件变成包数据,可以通过 importlib.resources.files('data')
访问它们。这应该与 "可编辑安装" 一起工作。您可能需要在您的打包文件 (setup.py
或 setup.cfg
或 pyproject.toml
) 中进行一些小的更改。
英文:
Create an empty data/__init__.py
file, so that data
becomes a top-level import package, so that the data files become package data, so that they are accessible via importlib.resources.files('data')
. This should work with "editable installation". You might need to do small changes in your packaging files (setup.py
or setup.cfg
or pyproject.toml
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论