Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

python - setuptools: adding additional files outside package

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

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?

Edit

The proposed solutions using something along the lines of

package_data = { '' : ['moredata.txt','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.

Solution

Based on the accepted answer, here's the solution

This is the 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='[email protected]',
    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,
)

And this is the MANIFEST.in:

include INFO.txt
graft additionalstuff
include somepackage/*.txt
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is also 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...