Python package : data files of a subpackage

Viewed 341

I have the following structure for a package :

├───Docs
│   ├───Dir
│   └───Output
└───MyPackage
    ├───__init__.py
    ├───test
    ├───MySubPackage1
    │   ├───__init__.py
    │   ├───...
    │   └───data
    │         └─── data.pth
    └───MySubPackage2
        └───__init__.py

I need to include the data.pth file so I tried as advise there to use a MANIFEST.IN like this :

MANIFEST.IN

graft data
setup.py

setup(name='MyPackage',
      version='1.0',
      package_dir={"MyPackage": "MyPackage"},
      packages=find_packages(),
      include_package_data=True)

but when installing :

python setup.py install

I see the following warning:

reading manifest template 'MANIFEST.in'
warning: no directories found matching 'data'

what is the correct way to handle this data that sits within a subpackage with a MANIFEST.IN ?

2 Answers

You can also use the package_data argument to setup() to explicitly note data files that must be included in your packages or sub-packages. This is how I usually go about it, and such files don't need to be included explicitly in MANIFEST.in:

setup(
    ...
    package_data={'MyPackage.MySubPackage': ['data.pth']}
)

for example. See https://setuptools.readthedocs.io/en/latest/userguide/datafiles.html

I think you might have another minor oops in your setup() which is package_dir={"MyPackage": "MyPackage"}.

package_dir is used to specify the top-level directory within your source tree in which a Python packages to be installed is found. By default it is '' (indicating the top-level of the source).

From the source tree you posted, it looks like MyPackage is already at the top-level of the source so you don't need this option at all; it's redundant. I believe that the way you've written it does work. But package_dir is more often used in cases where you have alternate source hierarchies. The most common is having a src/ directory your Python packages are found in, in which case you specify package_dir={'': 'src/'} which means all Python packages are found under src/. In your case I don't think you need this.

I thought setuptools was able to understand package and subpackages structure when reading the MANIFEST.in but this is apparently not the case.

Doing this :

MANIFEST.IN

graft MyPackage/MySubPackage1/data

solves the issue

Related