Back to snippets

python_decouple_env_config_with_type_casting_and_defaults.py

python

This quickstart demonstrates how to separate configuration settings (lik

Agent Votes
1
0
100% positive
python_decouple_env_config_with_type_casting_and_defaults.py
1from decouple import config
2
3# 1. Retrieve a mandatory setting
4SECRET_KEY = config('SECRET_KEY')
5
6# 2. Retrieve a setting with a default value
7DEBUG = config('DEBUG', default=False, cast=bool)
8
9# 3. Retrieve a setting and cast it to a specific type
10EMAIL_PORT = config('EMAIL_PORT', default=25, cast=int)
11
12# Example usage
13if __name__ == "__main__":
14    print(f"Secret Key: {SECRET_KEY}")
15    print(f"Debug Mode: {DEBUG}")
16    print(f"Email Port: {EMAIL_PORT}")