• Best practice for config files?

    From Michael F. Stemper@3:633/280.2 to All on Fri May 23 05:59:28 2025
    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.

    --- MBSE BBS v1.1.1 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Fri May 23 06:27:27 2025
    "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



    --- MBSE BBS v1.1.1 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Fri May 23 06:33:12 2025
    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.)



    --- MBSE BBS v1.1.1 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Paul Rubin@3:633/280.2 to All on Fri May 23 10:37:09 2025
    "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.

    --- MBSE BBS v1.1.1 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From Chuck Rhode@3:633/280.2 to All on Fri May 23 23:35:28 2025
    LS0tLS1CRUdJTiBQR1AgU0lHTkVEIE1FU1NBR0UtLS0tLQ0KSGFzaDogU0hBMQ0KDQpPbiBUaHUs IDIyIE1heSAyMDI1IDE0OjU5OjI4IC0wNTAwDQoiTWljaGFlbCBGL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REI1b0FBS0NSQmcyL3hpcEtPVw0KVWtnL0FKOWNXeHR5 a2VjZzMxSHZWckN6K0doV1BPbitnUUNlUGFjS2lTWEZsUmRrRWV5cnprc29iY2JUQ0Z3PQ0KPU96 ekQNCi0tLS0tRU5EIFBHUCBTSUdOQVRVUkUtLS0tLQ0K


    --- MBSE BBS v1.1.1 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Sat May 24 05:18:23 2025
    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.



    --- MBSE BBS v1.1.1 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Jason H@3:633/280.2 to All on Sat May 24 21:46:25 2025
    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

    --- MBSE BBS v1.1.1 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From Michael F. Stemper@3:633/280.2 to All on Sun May 25 01:05:01 2025
    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.


    --- MBSE BBS v1.1.1 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)