Python .env Magic
Let me paint you a scenario:?
You start developing your application and find yourself in need of specific environment variables. You then go into your IDE and set those variables or you start running “export SOME_VARIABLE=some-value”. A few hours later, you find yourself needing 10–50000 variables and the cycle repeats itself every time you go to sleep and wake up opening a new terminal and session.
If the above sounds like a tedious task, imagine you are working on more than one project at a time and each one requires multiple environment variables. Now you need to repeat everything from scratch for each terminal, each sessions, each project, each….. You get the idea!
Enter “.env”?file!
import os
from dotenv import load_dotenv
load_dotenv()
GCP_PROJECT_ID = os.getenv('GCP_PROJECT_ID')
SERVICE_ACCOUNT_FILE = os.getenv('SERVICE_ACCOUNT_FILE')
STORAGE_BUCKET_NAME = os.getenv('STORAGE_BUCKET_NAME')
This little feature will let you define and configure as much variables as you need for your local development for each projects whether it is 1 or 1000 without you doing anything ever again.
This file also will not be pushed to any VSC ensuring your variables will stay secured.
In addition because all of the needed variables are defined in one place for each project, it will make it so much easier to deploy it to your real environments without trying to locate and making a list of variables you need.
Hope this will make it easier for you to enjoy your development !