添加一个数据目录,放在Python包目录之外。

huangapple go评论69阅读模式
英文:

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 在构建时“移动”数据

通过 setuptoolspackage_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.pysetup.cfgpyproject.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).

huangapple
  • 本文由 发表于 2023年2月10日 04:04:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定