• Best practice for config files?

    From Michael F. Stemper@michael.stemper@gmail.com to comp.lang.python on Thu May 22 14:59:28 2025
    From Newsgroup: comp.lang.python

    I recently wrote a program to do some record-keeping for me. I found
    myself hard-coding a bunch of different values into it. This didn't
    seem right, so I made my first use of configparser.ConfigParser().
    Created the configuration file and everything is working fine.

    However, I wrote it based on the assumption that the program is
    running in the directory where the config file is stored, and has
    a specific name. I started having some second thoughts here.

    I thought about putting the location of the configuration file in
    the configuration file, but that seemed like a non-starter.[1]

    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?



    [1] See Tegan Jovanka in _Castrovalva_ for more on this idea.
    --
    Michael F. Stemper
    If it isn't running programs and it isn't fusing atoms, it's just bending space.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Thu May 22 20:27:27 2025
    From Newsgroup: comp.lang.python

    "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


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Thu May 22 20:33:12 2025
    From Newsgroup: comp.lang.python

    ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
    On Linux:
    ~/.config/YourAppName

    But don't just build this path manually as the proper
    code for Linux should follow the XDG convention. I hope
    that the library does this correctly!

    What I actually found on my harddisk was a "platformdirs"
    package from "setuptools", which seems to know about XDG.
    (I not sure whether the separated "platformdirs" from PyPI
    is the same.)


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Paul Rubin@no.email@nospam.invalid to comp.lang.python on Thu May 22 17:37:09 2025
    From Newsgroup: comp.lang.python

    "Michael F. Stemper" <michael.stemper@gmail.com> writes:
    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?

    You can also use an environment variable.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Lawrence D'Oliveiro@ldo@nz.invalid to comp.lang.python on Fri May 23 01:20:53 2025
    From Newsgroup: comp.lang.python

    On Thu, 22 May 2025 17:37:09 -0700, Paul Rubin wrote:

    "Michael F. Stemper" <michael.stemper@gmail.com> writes:

    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?

    You can also use an environment variable.

    Remember to follow some reasonable precedence order by which settings specified one way can be overridden by those specified another way. The
    most natural one would seem to be (from highest to lowest)

    1) command-line option
    2) environment variable
    3) user-specific config
    4) systemwide config
    5) hard-coded
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Chuck Rhode@CRhode@LacusVeris.com to comp.lang.python on Fri May 23 08:35:28 2025
    From Newsgroup: comp.lang.python

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On Thu, 22 May 2025 14:59:28 -0500
    "Michael F. Stemper" <michael.stemper@gmail.com> wrote:

    Is requiring the program to be executed in the directory containing
    the configuration file considered acceptable practice?

    Freedesktop.org proposes a specification for where such things ought
    to be located:

    + https://specifications.freedesktop.org/basedir-spec/latest/

    Here's how I do it:

    def get_xdg_config_home():

    """The configuration directory.

    Normally $HOME/.config.

    """

    result = pathlib.Path.home() / ".config"
    return result

    - --
    .. Be Seeing You,
    .. Chuck Rhode, Sheboygan, WI, USA
    .. Weather: https://LacusVeris.com/Wx
    .. 47° — Wind NNW at 12 mph. Sky clear.
    -----BEGIN PGP SIGNATURE-----

    iF0EARECAB0WIQT+MY/5I/LMPSswTbVg2/xipKOWUgUCaDB5oAAKCRBg2/xipKOW Ukg/AJ9cWxtykecg31HvVrCz+GhWPOn+gQCePacKiSXFlRdkEeyrzksobcbTCFw=
    =OzzD
    -----END PGP SIGNATURE-----
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Fri May 23 19:18:23 2025
    From Newsgroup: comp.lang.python

    ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
    - searching a sequence of locations for your config file

    Coincidentally, I just read:

    |When IDLE first starts, it attempts to read user
    |configuration files in ~/.idlerc/ (~ is one's home
    |directory).

    , just as another example.


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Lawrence D'Oliveiro@ldo@nz.invalid to comp.lang.python on Fri May 23 22:22:36 2025
    From Newsgroup: comp.lang.python

    On 23 May 2025 19:18:23 GMT, Stefan Ram wrote:

    Coincidentally, I just read:

    |When IDLE first starts, it attempts to read user |configuration files
    in ~/.idlerc/ (~ is one's home |directory).

    Apps should be abiding by the XDG spec nowadays, and stop cluttering up users’ home directories.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From rbowman@bowman@montana.com to comp.lang.python on Sat May 24 03:00:18 2025
    From Newsgroup: comp.lang.python

    On Fri, 23 May 2025 22:22:36 -0000 (UTC), Lawrence D'Oliveiro wrote:

    On 23 May 2025 19:18:23 GMT, Stefan Ram wrote:

    Coincidentally, I just read:

    |When IDLE first starts, it attempts to read user |configuration files
    in ~/.idlerc/ (~ is one's home |directory).

    Apps should be abiding by the XDG spec nowadays, and stop cluttering up users’ home directories.

    Good luck with that. I've got 48 directories ranging from .arduino15
    to .vscode. Then there are the 58 directories in .config.


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Jason H@jason_hindle@yahoo.com to comp.lang.python on Sat May 24 11:46:25 2025
    From Newsgroup: comp.lang.python

    On 22/05/2025 20:59, Michael F. Stemper wrote:
    I recently wrote a program to do some record-keeping for me. I found
    myself hard-coding a bunch of different values into it. This didn't
    seem right, so I made my first use of configparser.ConfigParser().
    Created the configuration file and everything is working fine.

    However, I wrote it based on the assumption that the program is
    running in the directory where the config file is stored, and has
    a specific name. I started having some second thoughts here.

    I thought about putting the location of the configuration file in
    the configuration file, but that seemed like a non-starter.[1]

    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?



    [1] See Tegan Jovanka in _Castrovalva_ for more on this idea.

    So, I use an environment variable because my config is shared between Python
    and Java auto test frameworks. I think keeping the config adjacent to the
    .py files is also workable because a Python program can know where it is:

    from pathlib import Path

    script_path = Path(__file__).resolve()
    script_directory = script_path.parent

    print(f"The script is located at: {script_path}")
    print(f"The script is located in the directory: {script_directory}")


    --
    A PICKER OF UNCONSIDERED TRIFLES
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Michael F. Stemper@michael.stemper@gmail.com to comp.lang.python on Sat May 24 10:05:01 2025
    From Newsgroup: comp.lang.python

    On 22/05/2025 15.27, Stefan Ram wrote:
    "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:

    [massive snip]

    On Linux:

    ~/.config/YourAppName

    [another one]


    Wow, if that's the best practice, I'll settle for second-best!

    Somebody who wished to remain anonymous contacted me via email and
    suggested that I could have my cake and eat it, too. I am going
    ahead with having a default location for the config file, as well
    as a command-line option to specify a different file. Blindingly
    obvious!

    And the default will not be in the directory in which the program
    is being run. Your post reminded me of the existence of $HOME/.config
    which is obviously the right place for it.

    Thanks for all of the suggestions.
    --
    Michael F. Stemper
    I refuse to believe that a corporation is a person until Texas executes one.

    --- Synchronet 3.21a-Linux NewsLink 1.2