如何将文件夹中的 sql 文件包含到 setuptools 中,同时打包为 python Egg

我的目录结构如下。


.

├── package

    ├── app

    │   ├── __init__.py

    │   ├── file1.py

    │   └── file2.py

    ├── master_sql_folder

    │   │

    │   │──sql_folder_1

    │   │      ├── world.sql

    │   │      ├── earth.sql

    │   │

    │   └──sql_folder_2

    │          ├── planet.sql

    │          ├── sun.sql

    │         

    └── wrapper_scripts

    │    ├── wrapper.py

    │    └── __init__.py

    ├── setup.py

我试图将 master_sql_folder 子文件夹( world.sql、earth.sql、planet.sql、sun.sql )中存在的所有 sql 文件包含到 setuptools 中,同时打包为 Egg,我无法在路径中使用 sql_folder_1 和 sql_folder_2 作为将来可以在 master_sql_folder 下添加新文件夹,我也需要代码来读取它们。我尝试将以下行添加到我的 setup.py 中,但它不包括构建中的 sql 文件。


package_data={'master_sql_folder':['*']}

packages=['app', 'wrapper_scripts']

我提前感谢您的帮助。


慕姐8265434
浏览 146回答 2
2回答

森林海

我尝试了几次迭代但没有成功。Often packages will need to depend on files which are not .py files: e.g. images, data tables, documentation, etc. Those files need special treatment in order for setuptools to handle them correctly.The mechanism that provides this is the MANIFEST.in file. This is relatively quite simple: MANIFEST.in is really just a list of relative file paths specifying files or globs to include.:include README.rstinclude docs/*.txtinclude funniest/data.jsonIn order for these files to be copied at install time to the package’s folder inside site-packages, you’ll need to supply include_package_data=True to the setup() function.超级容易做到,实际上只需将 MANIFEST.in 包含在与 setup.py 相同的目录中,并且根据文档,you’ll need to supply include_package_data=True to the setup() function.

侃侃尔雅

#sql_file_picker.py -- 将文件提供给 setup.py 的脚本import globclass FilePicker:    def __init__(self):        pass    def sql_file_picker(self):        sql_files = []        directories = glob.glob('master_sql_folder\\**\\')        for directory in directories:            files = glob.glob(directory + '*.sql')            if len(files) != 0:                sql_files.append((directory, files))        return sql_files在setup.py中from wrapper_scripts.sql_file_picker import FilePickerfrom setuptools import setupsetup(    name='XXXXXXXXX',    version='X.X.X',    packages=['app', 'wrapper_scripts'],    url='',    license='',    author='',    author_email='',    description='XXXXXXXXXXXXX',    data_files=FilePicker().sql_file_picker())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python