• Re: Best practice for config files?

    From Stefan Ram@21:1/5 to Stefan Ram on Thu May 22 20: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.)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Michael F. Stemper on Thu May 22 20: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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to Michael F. Stemper on Thu May 22 17: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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chuck Rhode@21:1/5 to All on Fri May 23 08:35:28 2025
    LS0tLS1CRUdJTiBQR1AgU0lHTkVEIE1FU1NBR0UtLS0tLQ0KSGFzaDogU0hBMQ0KDQpPbiBUaHUs IDIyIE1heSAyMDI1IDE0OjU5OjI4IC0wNTAwDQoiTWljaGFlbCBGLiBTdGVtcGVyIiA8bWljaGFl bC5zdGVtcGVyQGdtYWlsLmNvbT4gd3JvdGU6DQoNCj4gSXMgcmVxdWlyaW5nIHRoZSBwcm9ncmFt IHRvIGJlIGV4ZWN1dGVkIGluIHRoZSBkaXJlY3RvcnkgY29udGFpbmluZw0KPiB0aGUgY29uZmln dXJhdGlvbiBmaWxlIGNvbnNpZGVyZWQgYWNjZXB0YWJsZSBwcmFjdGljZT8NCg0KRnJlZWRlc2t0 b3Aub3JnIHByb3Bvc2VzIGEgc3BlY2lmaWNhdGlvbiBmb3Igd2hlcmUgc3VjaCB0aGluZ3Mgb3Vn aHQNCnRvIGJlIGxvY2F0ZWQ6DQoNCisgaHR0cHM6Ly9zcGVjaWZpY2F0aW9ucy5mcmVlZGVza3Rv cC5vcmcvYmFzZWRpci1zcGVjL2xhdGVzdC8NCg0KSGVyZSdzIGhvdyBJIGRvIGl0Og0KDQo+IGRl ZiBnZXRfeGRnX2NvbmZpZ19ob21lKCk6DQo+IA0KPiAgICAgIiIiVGhlIGNvbmZpZ3VyYXRpb24g ZGlyZWN0b3J5Lg0KPiANCj4gICAgIE5vcm1hbGx5ICRIT01FLy5jb25maWcuIA0KPiANCj4gICAg ICIiIg0KPiAgICAgDQo+ICAgICByZXN1bHQgPSBwYXRobGliLlBhdGguaG9tZSgpIC8gIi5jb25m aWciDQo+ICAgICByZXR1cm4gcmVzdWx0DQoNCi0gLS0gDQouLiBCZSBTZWVpbmcgWW91LA0KLi4g Q2h1Y2sgUmhvZGUsIFNoZWJveWdhbiwgV0ksIFVTQQ0KLi4gV2VhdGhlcjogIGh0dHBzOi8vTGFj dXNWZXJpcy5jb20vV3gNCi4uIDQ3wrAg4oCUIFdpbmQgTk5XIGF0IDEyIG1waC4gU2t5IGNsZWFy Lg0KLS0tLS1CRUdJTiBQR1AgU0lHTkFUVVJFLS0tLS0NCg0KaUYwRUFSRUNBQjBXSVFUK01ZLzVJ L0xNUFNzd1RiVmcyL3hpcEtPV1VnVUNhREI1b0FBS0NSQmcyL3hpcEtPVw0KVWtnL0FKOWNXeHR5 a2VjZzMxSHZWckN6K0doV1BPbitnUUNlUGFjS2lTWEZsUmRrRWV5cnprc29iY2JUQ0Z3PQ0KPU96 ekQNCi0tLS0tRU5EIFBHUCBTSUdOQVRVUkUtLS0tLQ0K

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Fri May 23 19: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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jason H@21:1/5 to Michael F. Stemper on Sat May 24 11: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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)