Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #197490

Re: Best practice for config files?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups comp.lang.python
Subject Re: Best practice for config files?
Date 22 May 2025 20:27:27 GMT
Organization Stefan Ram
Lines 83
Expires 1 Jun 2026 11:59:58 GMT
Message-ID <config-20250522212305@ram.dialup.fu-berlin.de> (permalink)
References <100nvn0$3kq1t$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de 9PC1rwRYtCdyR9jVISe0UA/PW2p1OhiIsxbcOlnzC0HyWp
Cancel-Lock sha1:CDb/LbAEZ6MzNxxfM3x0Xim/zGc= sha256:kqn/r/a6N2G8AYB6hiJi5C4jmGnTa7wkXT6pE2ngRn8=
X-Copyright (C) Copyright 2025 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed.
X-No-Archive Yes
Archive no
X-No-Archive-Readme "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access.
X-No-Html yes
Content-Language en-US
Xref csiph.com comp.lang.python:197490

Show key headers only | View raw


"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 | NextPrevious in thread | Next in thread | Find similar


Thread

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