DEV Community

John Higuita
John Higuita

Posted on

Manage environment variables with decouple in Python

In this post we're going to talk about how to set up environment variables for any Python project that we develop.

Python-decouple is a library that helps you separate the settings parameters from your source code. Its magic appears when the parameters related to an instance of the project, goes to an environment file. The parameters related to the project, goes straight to the source code.

Install

$ pip install python-decouple

Usage

The first one create a .env file in the root of your project.

TOKEN_KEY = 1232331sadsads8768:dsdsds32434-LJSAS08f
Enter fullscreen mode Exit fullscreen mode

If you are working with Git, update your .gitignore adding the .env file.
Then let's create cfg.py file.

from decouple import config
TOKEN = config('TOKEN_KEY')
Enter fullscreen mode Exit fullscreen mode

And finally, you already can to use the project' parameters in your main.py file.

from cfg import TOKEN
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
kovarpavel profile image
Pavel Kovar

Good hint, thanks