To get data installed relative to your Python module, you could use pkgutil.get_data()
or setuptools' pkg_resources.resource_string()
. This works even if your module is packaged as a zip archive.
To find out an appropriate place to put user data, you could use appdirs
module for portability:
import appdirs # $ pip install appdirs
user_data_dir = appdirs.user_data_dir("App name", "Author")
To avoid duplicating the logic of where to put data in a way that is optimal for your specific application, you could create an object that will be responsible for it. You could import it directly (in simple cases) e.g., from custom_app import config
or build it during an initialization and set it as an attribute of your application object app.config
or as a global function custom_app.get_config()
if there is none.
To get data path, ask the config
object: config.get_data_dir()
where config
may return values derived from config files (e.g., ~/.config/custom_app/config.yml
), environment variables (CUSTOM_APP_DATA_DIR
), command-line options with/without the help of pkgutil
/pkg_resources
/appdirs
modules.
It is not the only way to get the config info but it should be a good start that works in many cases.