'How can I find a resource file installed with setuptools data_files?
Problem
If I am installing a python package with pip and setuptools, and need to include a config file, this can be done with the setup argument data_files.
When installed with pip, this will install the file in a sensible location, such as on a Linux system at /usr/local/ as root or $HOME/.local with a non-root installation.
However I cannot seem to find a programmatic way to then determine the location to read the file in the python application.
Solutions that don't work
A similar question here suggested using
package_datainstead ofdata_fileswhich then allows you to find the file relative to the python file, however burying configuration files into python's site-packages directory hardly seems like a good user experience.One suggested approach is to use
sys.prefix, however this always returns the location of a root install, so if a user installs the package without root permissions, this will fail.sys.base_prefixhas the same behaviour.I have also looked at using the
pkg_resourceslibrary, but this also returns the path to the site-packages install location, not the config location.importlib.resourceshas the same behaviour.
Question
Is there a solution to finding the location of this installed config file that:
- is cross platform;
- works for both root and non-root installation;
- allows for the config file to be placed somewhere sensible (not in site-packages);
- doesn't require manually encoding all the possible paths?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
