How to load a python package resource from the current distribution using pkg_resources?

I have a Python package with some CSS style sheets that I have included as such resources:

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

Now I would 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 fixated on how to use it: How do I get the Distribution object for the current package?

+4
source share
1 answer

For this, there is a convenience method at the top level pkg_resources :

 import pkg_resources my_data = pkg_resources.resource_string(__name__, "my_style.css") 
+6
source

Source: https://habr.com/ru/post/1402195/


All Articles