8

I have a Python package with some css stylesheets which I have included as resources like so:

from setuptools import setup
setup(
    package_data={
        'my.package.name': ['*.css']
    }
    # ...
)

I would now like to load one of these included resources as a string. What is the best way to load a resource from the current package?

I see that the pkg_resources.Distribution object has a get_resource_string() method, but I am stuck on how to use this: How do I get a Distribution object for the current package?

Daniel Fortunov
  • 43,309
  • 26
  • 81
  • 106
  • If you scroll up a couple of lines from your own link, there is a section called '_Getting or Creating Distributions_'. – Gareth Latty Mar 19 '12 at 09:27

1 Answers1

9

There is a convenience method at the top level of pkg_resources for this:

import pkg_resources
my_data = pkg_resources.resource_string(__name__, "my_style.css")
Daniel Fortunov
  • 43,309
  • 26
  • 81
  • 106