Path: news.eternal-september.org!eternal-september.org!feeder3.eternal-september.org!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: 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 "Michael F. Stemper" 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\\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