Explain Codes LogoExplain Codes Logo

What's the best practice using a settings file in Python?

python
best-practices
settings
environment-variables
Alex KataevbyAlex Kataev·Aug 14, 2024
TLDR

One of the best practice in Python is to create a config.py that contains constants. Simply use from config import * so you can access these constants everywhere.

# config.py # Fun fact: I stop at nothing to debug my code! DEBUG = True # DATABASE: a place where SQL queries come to party! DATABASE = 'prod_db' # main.py from config import * print(DEBUG, DATABASE) # prints "True prod_db"

For secrets, you can use environment variables with os.environ.

import os # Why did the secret key go to therapy? Because it had hidden issues! SECRET_KEY = os.environ.get('SECRET_KEY', 'default')

Now, you have a separated, secured, and easily accessible configuration.

JSON, YAML, or configparser: Which one to choose?

When we talk about nested settings, you need to choose the format wisely. JSON is natively supported from Python 2.5 onwards for easy parsing and serialization with json.load() and json.dump(). However, for complicated nesting or multi-line strings, YAML which is a superset of JSON, is a better choice.

For a structured approach Python's configparser, supports sections for easy management. The choice highly depends on coding requirements and readability.

Structuring settings for future growth

To make your nested settings scalable, keep the format consistent. Having separate modules for categorized settings (like database, user interface, network) helps in maintainability as your projects grow. Here's an example:

# db_settings.py # DB admins do it in tables! DATABASE = { 'name': 'prod_db', 'port': 5432, 'host': 'localhost', } # ui_settings.py # I speak Fluent (Programming) languages UI = { 'theme': 'light', 'language': 'en', } # Import them where needed from db_settings import DATABASE from ui_settings import UI

You might want to consider YAML for cross-language support with PyYAML, keep it human-friendly.

Security and maintainability for the long haul

When dealing with sensitive data like API keys and passwords, it's always best practice to use environment variables or encryption tools like git-crypt. For future updates, choose plain text files or formats that support version control for reflect changes effectively.

Overcoming challenges with community libraries

There are community-supported libraries like Dynaconf that makes configuration easier with dynamic management, support for multiple file formats and are easily integrated to your existing code base. They offer advanced features like settings validation and type casting, which vastly improves the developer experience and robustness of your configuration patterns.