使用setuptools(setup.py)在主模块之外添加文件夹和子文件夹 [英] Add folders and subfolders outside of main module using setuptools(setup.py)

查看:527
本文介绍了使用setuptools(setup.py)在主模块之外添加文件夹和子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,它是与 setuptools> setuptools:在外部添加其他文件类似的问题包包含带有setup.py的非Python文件.我的结构与第一个问题非常相似

So it is yet another similar looking but different question than setuptools: adding additional files outside package and Including non-Python files with setup.py. I have structure very similar to that of first question

-module
   -python_file1.py
   -python_file2.py
-folder
   -subfolder1
      -data_file_1.txt
   -subfolder2
      -data_file_2.txt

我想要的东西:我要安装软件包以及其中的文件夹,子文件夹和文件.

What I want: I want to install the packages along with the folder,subfolders and files within them.

我尝试过的事情:

  • 方法_1:如果将文件夹移动到模块内部,则可以轻松使用package_data选项,例如
  • Approach_1: If I move the folder inside the module then I can easily use package_data option like

package_data = {'':['folder/**/*']},但这将迫使我将结构更改为更混乱的结构.想象一下,我有10-15个子文件夹.

package_data = {'':['folder/**/*']} but this will force me to change the structure to a more messy one. Imagine I have 10-15 sub folders.

  • 方法_2:通过使用data_files选项,我可以简单地使用glob.glob('my_repo')扫描整个存储库来列出所有文件,包括文件夹/子文件夹和文件,但是由于我无法控制(或者我不知道一种)目标目录(取决于不同的操作系统),因此我无法将文件移动到正确的目标目录中.
  • Approach_2: By using data_files option I could list all the files including folders/sub folders and file by simply scanning the entire repo using glob.glob('my_repo') but since I have no control over(or maybe I'm not aware of one) the target directory depending on different OS so I am unable to move the files in the correct target dir.

我正在为这两种方法寻找一种优雅的解决方案.样本setup.py文件仅供参考:

I'm looking for an elegant solution for either of the approaches. Sample setup.py file just for reference:

from setuptools import setup, find_packages

setup(
    name='your_project_name',
    version='0.1',
    description='A description.',
    packages=find_packages(),
    package_data={'': ['folders/**/*'},
    include_package_data=True,
    install_requires=[],
)

推荐答案

我相信类似以下的内容可以帮助您:

I believe something like the following could get you there:

.
├── data
│   ├── file.txt
│   └── foo
│       ├── bar
│       │   └── file.txt
│       └── file.txt
├── MANIFEST.in
├── setup.cfg
├── setup.py
└── src
    └── thing
        ├── __init__.py
        └── nothing
            └── __init__.py

MANIFEST.in

# ...
recursive-include data *.txt

setup.py

#!/usr/bin/env python3

import pathlib
import setuptools

def main():
    data_packages = [
        'thing/{}'.format(p).replace('/', '.')
        for p
        in pathlib.Path('data').glob('**')
    ]
    packages = setuptools.find_packages(where='src')
    package_dir = {
        'thing': 'src/thing',
        'thing.data': 'data'
    }

    setuptools.setup(
        # see 'setup.cfg'
        packages=packages + data_packages,
        package_dir=package_dir,
        include_package_data=True,
    )

if __name__ == '__main__':
    main()

*.txt被打包为Python项目的thing.data包下的包数据.可以像这样读取数据:

The *.txt are packaged as package data under the thing.data package of the Python project. The data can be read for example like this:

python3 -c "import pkgutil; print(pkgutil.get_data('thing', 'data/foo/bar/file.txt').decode())"

这篇关于使用setuptools(setup.py)在主模块之外添加文件夹和子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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