Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #197490
| From | ram@zedat.fu-berlin.de (Stefan Ram) |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Best practice for config files? |
| Date | 2025-05-22 20:27 +0000 |
| Organization | Stefan Ram |
| Message-ID | <config-20250522212305@ram.dialup.fu-berlin.de> (permalink) |
| References | <100nvn0$3kq1t$1@dont-email.me> |
"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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
Best practice for config files? "Michael F. Stemper" <michael.stemper@gmail.com> - 2025-05-22 14:59 -0500
Re: Best practice for config files? ram@zedat.fu-berlin.de (Stefan Ram) - 2025-05-22 20:27 +0000
Re: Best practice for config files? ram@zedat.fu-berlin.de (Stefan Ram) - 2025-05-22 20:33 +0000
Re: Best practice for config files? ram@zedat.fu-berlin.de (Stefan Ram) - 2025-05-23 19:18 +0000
Re: Best practice for config files? (Posting On Python-List Prohibited) Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-05-23 22:22 +0000
Re: Best practice for config files? (Posting On Python-List Prohibited) rbowman <bowman@montana.com> - 2025-05-24 03:00 +0000
Re: Best practice for config files? "Michael F. Stemper" <michael.stemper@gmail.com> - 2025-05-24 10:05 -0500
Re: Best practice for config files? Paul Rubin <no.email@nospam.invalid> - 2025-05-22 17:37 -0700
Re: Best practice for config files? (Posting On Python-List Prohibited) Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-05-23 01:20 +0000
Re: Best practice for config files? Chuck Rhode <CRhode@LacusVeris.com> - 2025-05-23 08:35 -0500
Re: Best practice for config files? Jason H <jason_hindle@yahoo.com> - 2025-05-24 11:46 +0000
csiph-web