setuptools:在包外添加额外的文件 [英] setuptools: adding additional files outside package

查看:47
本文介绍了setuptools:在包外添加额外的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 应用程序,它具有我无法更改的固定布局.我想使用 setuptools 将其打包,例如编写一个 setup.py 脚本.

I have a python application that has a fixed layout which I cannot change. I would like to wrap it up using setuptools, e.g. write a setup.py script.

使用官方文档,我能够编写第一个模板.但是,有问题的应用程序使用了许多额外的数据文件,这些文件不是任何包的明确组成部分.这是一个示例源代码树:

Using the official documentation, I was able to write a first template. However, the application in question uses a lot of additional data files that are not explicitly part of any package. Here's an example source tree:

somepackage
   __init__.py
   something.py
   data.txt
additionalstuff
   moredata.txt
INFO.txt

麻烦来了:something.py 中的代码读取文件moredata.txtINFO.txt.对于前者,我可以通过添加一个空的 additionalstuff/__init__.py 文件来将 additionalstuff 提升到一个包并让 setuptools<获取它来解决问题./代码>.但是我怎么可能将 INFO.txt 添加到我的 .egg 中?

Here's the trouble: The code in something.py reads the files moredata.txt and INFO.txt. For the former, I can monkeypatch the problem by adding an empty additionalstuff/__init__.py file to promote additionalstuff to a package and have it picked up by setuptools. But how could I possibly add INFO.txt to my .egg?

使用类似以下内容的建议解决方案

The proposed solutions using something along the lines of

package_data = { '' : ['moredata.txt','INFO.txt']}

对我不起作用,因为文件 moredataINFO.txt 不属于一个包,而是一个单独文件夹的一部分,该文件夹只是模块作为一个整体,而不是任何单独的包.如上所述,在 moredata.txt 的情况下,这可以通过将 __init__.py 文件添加到 additionpythonalstuff 来解决,从而将其提升为一袋.然而,这不是一个优雅的解决方案,对于位于顶级目录中的 INFO.txt 根本不起作用.

does not work for me because the files moredata and INFO.txt do not belong to a package, but are part of a separate folder that is only part of the module as a whole, not of any individual package. As explained above, this could be fixed in the case of moredata.txt by adding a __init__.py file to additionpythonalstuff, thus promoting it to a package. However, this is not an elegant solution and does not work at all for INFO.txt, which lives in the top-level directory.

根据接受的答案,这是解决方案

Based on the accepted answer, here's the solution

这是setup.py:

from setuptools import setup, find_packages

setup(
    name='mytest',
    version='1.0.0',
    description='A sample Python project',
    author='Test',
    zip_safe=False,
    author_email='test@test.com',
    keywords='test',
    packages=find_packages(),
    package_data={'': ['INFO.txt', 'moredata.txt'],
                  'somepackage':['data.txt']},
    data_files=[('.',['INFO.txt']),
                ('additionalstuff',['additionalstuff/moredata.txt'])],
    include_package_data=True,
)

这是MANIFEST.in:

include INFO.txt
graft additionalstuff
include somepackage/*.txt

推荐答案

还有data_files

data_files=[("yourdir",
             ["additionalstuff/moredata.txt", "INFO.txt"])],

考虑一下要将这些文件放在哪里.文档中的更多信息.

Have a think about where you want to put those files. More info in the docs.

这篇关于setuptools:在包外添加额外的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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