"Michael F. Stemper" <
michael.stemper@gmail.com> wrote or quoted:
Should I specify the location of the config file with a command-line
option, or is requiring the program to be executed in the directory
containing the configuration file considered acceptable practice?
It was me who digged out this "platformdirs" "user_config_dir"
API using a source code search on my harddisk without any help.
But then I asked my buddy, the chatbot, to explain how to
use it, which I include here, followed by some more words
of my own at the end. Chatbot:
To use the platformdirs API - specifically the user_config_dir
function - to get the correct user configuration directory
for your application (cross-platform), follow these steps:
1. Install the platformdirs package: bash:
pip install platformdirs
2. Use user_config_dir in your Python code:
from platformdirs import user_config_dir
config_dir = user_config_dir(appname="YourAppName", appauthor="YourCompany")
print(config_dir)
The name "appname" is the name of your application.
The name "appauthor" is usually your company or organization
name (optional, but recommended for Windows).
You can also specify "version", "roaming", and "ensure_exists"
as optional arguments.
This function returns the path to the appropriate user-specific
configuration directory for the running platform:
On macOS:
~/Library/Application Support/YourAppName
On Windows:
C:\Users\<User>\AppData\Local\YourCompany\YourAppName
On Linux:
~/.config/YourAppName
Example: Python:
config_dir = user_config_dir(appname="MyApp", appauthor="MyCompany")
print(config_dir)
# Output will be platform-specific, e.g., /home/user/.config/MyApp on Linux
This ensures your app stores configuration files in the right
place on any OS.
(End of the chatbot's explanation, formatted for Usenet manually
and slightly edited by me - S.R.)
You also might think about:
- reading a CONFIG_PATH for you app for an environment
variable if set
- searching a sequence of locations for your config file
which might include the current directory and the
directories from other methods and using the first config
file found
- having an "installation" dialog: If your program can't
find its config file in any of the places it is looking
into, then it assumes it is run for the first time and
asks the user where (from the places in the preceding
paragraph) to store it, it will find it the next time
it does the activity from the preceding paragraph
- having an "uninstall" dialog: If your user chooses the
uninstall activity, the program will rename or delete
its config files