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
?
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.
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
In Fedora, our pip package Recommends setuptools. Practically that means: Majority of users who install pip will get setuptools by default. Users can explicitly uninstall setuptools after installing pip or exclude setuptools when installing pip.
Place the files that you want to include in the package directory (in our case, the data has to reside in the roman/ directory). Add the field include_package_data=True in setup.py. Add the field package_data={'': [... patterns for files you want to include, relative to package dir...]} in setup.py .
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.
You need to use package_data
. Put your setup.py
in the root directory, then you just need to:
package_data={'': [ 'somepackage/*.txt', 'additionalstuff/*.txt', '*.txt', ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With