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
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')
And finally, you already can to use the project' parameters in your main.py
file.
from cfg import TOKEN
Top comments (1)
Good hint, thanks