> For the complete documentation index, see [llms.txt](https://docs.santoshpurbey.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.santoshpurbey.com/django/python-decouple.md).

# 💦Python-Decouple

## Python Decouple

*Decouple* helps you to organize your settings so that you can change parameters without having to redeploy your app.

It also makes it easy for you to:

1. store parameters in *ini* or *.env* files;
2. define comprehensive default values;
3. properly convert values to the correct data type;
4. have **only one** configuration module to rule all your instances.

It was originally designed for Django but became an independent generic tool for separating settings from code.

### Install:

{% tabs %}
{% tab title="pip" %}

```bash
pip install python-decouple
```

{% endtab %}

{% tab title="pipenv" %}

```bash
pipenv install python-decouple
```

{% endtab %}
{% endtabs %}

Then use it on your `settings.py.`

### Import the config object:

```python
from decouple import config, Csv
```

### Retrieve the configuration parameters:

```python
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', default=False, cast=bool)
```

### For Allowed Host:

```python
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default='127.0.0.1', cast=Csv())
```

### Postgres database config:

{% code title="settings.py" %}

```python
DATABASES = {
    "default": {
        "ENGINE": config("ENGINE"),
        "NAME": config("DATABASE"),
        "USER": config("USER"),
        "PASSWORD": config("PASSWORD"),
        "HOST": config("HOST", "localhost"),
        "PORT": config("PORT", "5432"),
    }
}

```

{% endcode %}

{% hint style="info" %}
**Decouple** supports both *`.ini`*&#x61;nd *`.env`* files.
{% endhint %}

{% tabs %}
{% tab title=".env" %}

```bash
DEBUG=True
ALLOWED_HOST='.localhost,'
SECRET_KEY=ARANDOMSECRETKEY
ENGINE=django.db.backends.postgresql
DATABASE=<dbname>
USER=<db_username>
PASSWORD=<db_password>
HOST=db
PORT=5432
# This is comment 
```

{% endtab %}

{% tab title="config.ini" %}

```bash
[settings]
DEBUG=True
ALLOWED_HOST='.localhost,'
SECRET_KEY=ARANDOMSECRETKEY
ENGINE=django.db.backends.postgresql
DATABASE=<dbname>
USER=<db_username>
PASSWORD=<db_password>
HOST=db
PORT=5432

```

{% endtab %}
{% endtabs %}
