What is Python-Decouple?
Python-Decouple is a simple yet powerful tool that helps manage Django configuration settings securely. Instead of hardcoding secrets like SECRET_KEY or database credentials in settings.py, it allows you to store them in environment variables or a .env file, keeping your codebase clean and safe.
Why Use Python-Decouple?
- Better security – Keeps sensitive data out of your code.
- Easy environment switching – Quickly adapt configurations for development, staging, and production.
- Less clutter in settings.py – Keeps settings organized and modular.
- Automatic type conversion – Easily manage strings, integers, and lists.
- Seamless integration – Works with Django, CI/CD pipelines, and containerized deployments.
Quick Commands
# Install Python-Decouple
uv add python-decouple
# Create a .env file and store secrets
SECRET_KEY=mysecretkey
DEBUG=True
DATABASE_NAME=mydatabase
DATABASE_USER=myuser
DATABASE_PASSWORD=mypassword
# Use Python-Decouple in settings.py
from decouple import config
SECRET_KEY = config("SECRET_KEY")
DEBUG = config("DEBUG", default=False, cast=bool)
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": config("DATABASE_NAME"),
"USER": config("DATABASE_USER"),
"PASSWORD": config("DATABASE_PASSWORD"),
"HOST": "localhost",
"PORT": 5432,
}
}
📖 Read the full article on Medium
🔗 Follow me on:
🌍 My Website
💻 GitHub
🤝 LinkedIn
✍️ Medium
Top comments (0)