pythonegg

Accessing data files from within an egg


I have a package with a default json file in case the user hasnt supplied one data = pkgutil.get_data(__name__, "deprecated.json")

when running stand-alone this works fine

But if I run python setup.py install to create my.egg The run easy_install my.egg Finally start the executable I get an error message [Errno 2 nosuch file or director: `d:\\work\\pythonvenv\\lib\\site-packages\\my.egg\\PackageName\\depercate.json

    return loader.get_data(resource_name)
  File "<frozen importlib._bootstrap_external>", line 972, in get_data

I thought the purpose of using pkgutil.get_data was to prevent this type of issue?


Solution

  • Modify your setup.py file to include package_data as in https://docs.python.org/3.8/distutils/setupscript.html#installing-package-data

    setup(
        ...,
        package_data={'pkgName': ["path/to/deprecated.json"]},
    )
    

    Another way to achieve the same behavior is to create a MANIFEST.in file with the contents include path/to/deprecated.json and then in your setup function include the argument include_package_data=True. This is detailed in https://python-packaging.readthedocs.io/en/latest/non-code-files.html.