Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including data files with setup.py

I'm having trouble including data files in my setup.py script. My package is setup as follows:

my_package/
    setup.py
    MANIFEST.in

    my_package/
        __init__.py
        access_data.py

        data_files/
            my_data_file.csv

I want to include the my_data_file.csv file when installing so that it can be read by access_data.py. To do so I used the package_data keyword in setuptools:

setup(...,
      packages=['my_package'],
      package_data={'my_package': ['./my_package/data_files/my_data_file.csv']},
      include_package_data=True
      )

I also included the file in MANIFEST.in:

recursive-include my_package/data_files *

setup.py seems to run fine and doesn't throw any errors. However, when I import the package I get a file not found error because my_data_file.csv is missing. I have tried referencing other stack overflow questions (particularly this one) but can't figure out what I'm doing wrong. How can I get setup.py to include the necessary data files?

like image 803
user144153 Avatar asked Jul 17 '17 15:07

user144153


1 Answers

If it is listed in setup.py's package_data (correctly) you shouldn't need to include it in MANIFEST.in (as it will be included automatically)

In your case, the error is with your package_data line, the paths are relative to the namespace's root

In your case it should be:

package_data={'my_package': ['data_files/my_data_file.csv']},

Also note that the key in package data is the dotted module path (it's not super relevant for this toy case however).

like image 53
Anthony Sottile Avatar answered Nov 06 '22 10:11

Anthony Sottile