Python .env Magic

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!

  • Create a ".env" file inside your project's folder

  • Configure your variables inside the file

  • Use “dotenv” library to load this file every time you run your code and get the values for the variables using the "os" library

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 !

要查看或添加评论,请登录

Evgeni (John) Biriukov的更多文章

  • Compostions Super Power

    Compostions Super Power

    I got something really cool in store for you! We will continue from where we left of using GCP provider:…

    1 条评论
  • Python: Pydantic

    Python: Pydantic

    Pydantic is a data validation and settings management library for Python. It uses Python’s type annotations to perform…

  • Crossplane in 2024

    Crossplane in 2024

    Visit my repository for this article: https://github.com/JohnMops/DevOps/tree/main/kubernetes/crossplane Introduction…

    6 条评论
  • Python Tips for a cleaner code

    Python Tips for a cleaner code

    Tip #1: Never forget to add code to an empty function again When working on your code, you are probably used to fill…

    1 条评论
  • StatefulSets - Resizing PVC

    StatefulSets - Resizing PVC

    One of the most important features of a k8s STS is the ability to attach volumes to the pods and use them as a…

    1 条评论

社区洞察

其他会员也浏览了