Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setuptools: adding additional files outside package

Tags:

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 
like image 358
carsten Avatar asked Sep 16 '15 12:09

carsten


People also ask

Does PIP depend on Setuptools?

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.

How do I include a file in a Python package?

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 .


2 Answers

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.

like image 96
pacholik Avatar answered Oct 06 '22 09:10

pacholik


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', ] 
like image 36
Daniele Pantaleone Avatar answered Oct 06 '22 10:10

Daniele Pantaleone