python-decouple

repository·master·Indexed 25 days ago

https://github.com/hbnetwork/python-decouple

A tool to separate settings from code by managing instance-specific parameters via .env or .ini files. It provides type casting, default values, and support for environment variable overrides. Includes helpers like Csv for list conversion and Choices for validation, as well as RepositoryEnv for custom file paths.

Tokens
1.3K
Snippets
10
Records
12
Agent score
35%

What's inside python-decouple

  1. Store settings in an .ini file

    master

    Create a settings.ini file next to your configuration module. Decouple supports string interpolation via ConfigParser. To represent a literal % character, you must escape it as %%.

    [settings]
    DEBUG=True
    TEMPLATE_DEBUG=%(DEBUG)s
    SECRET_KEY=ARANDOMSECRETKEY
    DATABASE_URL=mysql://myuser:mypassword@myhost/mydatabase
    PERCENTILE=90%%
    #COMMENTED=42
  2. Specify a custom path for the .env file

    master

    To use a .env file located at a specific path, instantiate RepositoryEnv with the desired path and pass it to the Config class.

    import os
    from decouple import Config, RepositoryEnv
    
    config = Config(RepositoryEnv("path/to/.env"))
  3. Configure custom encoding for config

    master

    Decouple defaults to UTF-8. Since config is lazy, you can change the encoding immediately after import before any configuration is actually accessed.

    from decouple import config
    import locale
    
    # Set a specific encoding
    config.encoding = 'cp1251'
    SECRET_KEY = config('SECRET_KEY')
    
    # Or use the system default encoding
    config.encoding = locale.getpreferredencoding(False)
    SECRET_KEY = config('SECRET_KEY')
  4. Use the Csv helper for list conversion

    master
    The Csv helper allows you to parse comma-separated strings into lists or tuples. You can pass a cast argument to the Csv constructor to transform the individual elements, and use post_process to change the final container type (e.g., to a tuple).
  5. Use multiple environment files together

    master

    To merge multiple environment files, use collections.ChainMap to wrap multiple RepositoryEnv instances. This allows you to layer configuration (e.g., a private file on top of a standard .env file).

    from collections import ChainMap
    from decouple import Config, RepositoryEnv
    
    config = Config(ChainMap(RepositoryEnv(".private.env"), RepositoryEnv(".env")))
  6. Use the config object to retrieve parameters

    master

    Import the config object to retrieve configuration parameters. You can provide a default value to prevent errors if the key is missing, and a cast argument to convert the string value into a specific Python type (like bool, int, or a custom callable).

    from decouple import config
    
    SECRET_KEY = config('SECRET_KEY')  # Raises UndefinedValueError if missing
    DEBUG = config('DEBUG', default=False, cast=bool)
    EMAIL_HOST = config('EMAIL_HOST', default='localhost')
    EMAIL_PORT = config('EMAIL_PORT', default=25, cast=int)
  7. Define the .env file path via an environment variable

    master

    You can dynamically determine the path to your environment file by reading an environment variable (e.g., DOTENV_FILE) using os.environ.get.

    import os
    from decouple import Config, RepositoryEnv
    
    DOTENV_FILE = os.environ.get("DOTENV_FILE", ".env") # only place using os.environ
    config = Config(RepositoryEnv(DOTENV_FILE))