Dee Code

Working with env files in Python

I use env files in most of my projects to store values which should not be committed to code. Env files are also an excellent way to manage multiple settings related to different environments.

python-dotenv library makes working with .env files a breeze.

pip install python-dotenv

Given my .env file has these settings

REGION=ap-southeast-2
PROFILE=dev
SECRET_ARN=arn:aws:blahblah

This code will get the values from .env file and put it in local variables

import os
from dotenv import load_dotenv

load_dotenv()

REGION = os.getenv('REGION')
PROFILE = os.getenv('PROFILE')
SECRET_ARN = os.getenv('SECRET_ARN')

Tags

Tech Blogging AWS Python VIM Tools FastAPI Mac Hardware React JavaScript Ruby RoR TypeScript Personal Work Career Algorithms Productivity