Re: Best practice for config files?

Liste des GroupesRevenir à cl python 
Sujet : Re: Best practice for config files?
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.python
Date : 22. May 2025, 21:27:27
Autres entêtes
Organisation : Stefan Ram
Message-ID : <config-20250522212305@ram.dialup.fu-berlin.de>
References : 1
"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



Date Sujet#  Auteur
22 May 25 * Best practice for config files?11Michael F. Stemper
22 May 25 +* Re: Best practice for config files?6Stefan Ram
22 May 25 i+- Re: Best practice for config files?1Stefan Ram
23 May 25 i+* Re: Best practice for config files?3Stefan Ram
23 May 25 ii`* Re: Best practice for config files? (Posting On Python-List Prohibited)2Lawrence D'Oliveiro
24 May 25 ii `- Re: Best practice for config files? (Posting On Python-List Prohibited)1rbowman
24 May 25 i`- Re: Best practice for config files?1Michael F. Stemper
23 May 25 +* Re: Best practice for config files?2Paul Rubin
23 May 25 i`- Re: Best practice for config files? (Posting On Python-List Prohibited)1Lawrence D'Oliveiro
23 May 25 +- Re: Best practice for config files?1Chuck Rhode
24 May 25 `- Re: Best practice for config files?1Jason H

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal