Open python configuration file located inside the package

I am trying to distribute a python package with a file other than python. The file is in mypackge / config.yml, and I also added it to manifest.in (recursive-include mypackage * .yml). When I run the python setup.py installation, it is apparently added to the egg, but I do not know how to access the file inside the code. open(os.path.join('mypackage', 'config.yml'))will stop working as soon as I exit the directory where setup.py ...

Is there a way to securely access the file inside the egg, for example, the Java method getResourceAsStream?

+4
source share
1 answer

, , , pkg_resources, setuptools , :

import pkg_resources
my_data = pkg_resources.resource_string(__name__, "config.yml")

, . , __name__.

+5

All Articles