[FIXED] access environment variables in Python inside many files

Issue

if i need to get the value of an environment variables inside many files in the project Python (flask framework) should i use os.environ[‘HOME’] for every use or there is better way?

for example:
auth\routes.py

username= os.environ.get("USERNAME")

and in routes under posts
posts\routes.py

username= os.environ.get("USERNAME")

Solution

Write a common file to read all your environmental values.

config.py

import os

USERNAME = os.environ.get("USERNAME")

Then you can reuse it like this,

auth\routes.py

import config

username = config.USERNAME

Answered By – Rahul K P

Answer Checked By – Dawn Plyler (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published