Trouble packaging static file with Python package

30 Views Asked by At

I created a Python package for functionality that will have different clients.

Everything works perfectly in that my command-line app is able to import the package and successfully call the package's functions.

Now, my package needs to read some static files that I want to ship with the package.

I've read and attempted to use the code from numerous articles and questions/answers - especially regarding setuptools, pkg_resources, and importlib.resources. However, every one of them raise an exception stating that my file doesn't exist. I also tried pkgutil without success.

import pkgutil
data = pkgutil.get_data(__name__, 'templates/data.md')
# I also tried many variations with the path.
# For example: 'foo/templates/data.md' also fails.

The fact that I can't get any solution to work has me wondering if I'm even bundling my file correctly.

I have the following package layout:

foo
  foo
    __init__.py
    (many other .py files)
    templates
      data.md
  README.md
  requirements.txt
  setup.py

Here is my setup.py file:

import setuptools 
  
with open("README.md", "r") as fh: 
    description = fh.read() 
  
setuptools.setup( 
    name="foo", 
    version="0.0.1",
    packages=["foo", "foo.templates"], 
    include_package_data=True,
    install_requires=[],
    package_data={'foo': ['foo/templates/*']},
)

Is my setup call incorrect? How do I access data.md at runtime?

0

There are 0 best solutions below