can somebody please provide me with an example of ...
# general
m
can somebody please provide me with an example of using
resources()
in a python context? i have a few openapi specs i want to include but when they make it into the pythonpath, they are packageless so
pkgutil.get_data
can't find them.
using
python_library()
worked but eugh it just feels wrong
h
Can you post your BUILD file?
The files in
resources()
should be in the package implied by their location in the source tree under their source root
E.g., we have a BUILD file containing
Copy code
python_library(...
  dependencies = [
    :somefiles
  ]
)

resources(name='somefiles',
  sources=['foo.txt', 'bar.txt'])
Where foo.txt and bar.txt are in the same dir as the BUILD file. Then we can access them from code in the sibling library with e.g.,
pkg_resources.resource_string(__name__, 'foo.txt')
I assume the pkgutil equivalent should work
m
what happens when they are being used by a non-sibling library
h
Then you provide the appropriate package name to
pkg_resources.resource_string()
Siblings are convenient because then
__name__
works
Passing a module name to
pkg_resources.resource_string()
means that the resource names are interpreted as relative to that module
So we typically have a method in a sibling module that gets the resource, and other code calls that method
But you can pass whatever module name you like