https://pantsbuild.org/ logo
#general
Title
# general
m

miniature-dawn-49992

07/31/2019, 6:17 PM
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

happy-kitchen-89482

07/31/2019, 7:45 PM
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

miniature-dawn-49992

07/31/2019, 8:43 PM
what happens when they are being used by a non-sibling library
h

happy-kitchen-89482

08/01/2019, 3:47 PM
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
5 Views