Cross-platform addressing of the config file
Current answers provide incomplete solution to your problems (cross-platform + user vs system-wide config).
confuse library answers your needs :
Combine configuration data from multiple sources. Using layering, Confuse allows user-specific configuration to seamlessly override system-wide configuration, which in turn overrides built-in defaults.
[...]
An in-package config_default.yaml can be used to provide bottom-layer defaults using the same syntax that users will see. A runtime overlay allows the program to programmatically override and add configuration values. Look for configuration files in platform-specific paths. Like $XDG_CONFIG_HOME or ~/.config on Unix; “Application Support” on macOS; %APPDATA% on Windows.
You may want to use dirspec
. It works in GNU/Linux, Mac OS and Windows.
You can get it from: Launchpad
Or installing it from PyPI
pip install dirspec
and in your code use something like:
from dirspec.basedir import get_xdg_config_home
config_path = get_xdg_config_home()
It's used by Ubuntu One, look at this code example from their documentation: https://one.ubuntu.com/developer/data/u1db/tutorial#storing-and-retrieving-tasks
Python will allow forward-slash paths on Windows, and os.path.expanduser
works on Windows also, so you can get a user-specific file path using:
config_file = os.path.expanduser("~/foo.ini")
if you want to find a .ini in the user's home directory. I'm not sure how to unify file-based .ini and registry settings.