Search code examples
pythonpython-packaging

Python Package - Storing Configuration/Settings


I'm in the process of creating a python package to interact with various components of the Salesforce API (similar to simple-salesforce but with a more scalable architecture so developers can add support for non-core Salesforce APIs.

In the past, I required users to create a series of environment variables to store their salesforce credentials, but I recently received some requests to build out multi-org support and allow credential storage in AWS's secret manager rather than environment variables.

I'm testing out a new architecture, in which I require users to edit a config.py in the package directory and add their salesforce org's specific settings. That way, users can choose how many orgs they define, and whether they want to store credentials using environment variables or a secrets manager etc.

The issue is, when I push a new release and users upgrade the package via pip, the config.py is wiped clean with the update. Is there a best practice for storing user settings? I've used django before and envisioned an infrastructure similar to django's settings.py (source), but realized only after I built my package that the django settings file is typically defined at the project-level rather than the package-level (and I don't see a huge value-add outside of settings management to adopt a similar structure for my package).

Has anyone found alternative, successful approaches to managing settings or configurations?


Solution

  • Library configuration is best stored outside the library source, in a user's home directory under the user-specific configuration folder; on Linux, this is ~/.config. You can use the appdirs library to determine the proper user configuration location for your project for the user's OS, and then just have the user place the config file at that location where it is read by your library.