← Courses

Python for AI — Beginner Foundations

Data Science

Tools · Lesson 68 of 76

Using .env files

Typing export every time is tedious, so projects keep their settings in a file named .env:

API_KEY=abc123
APP_MODE=development

A small package called python-dotenv loads that file into the environment when your program starts:

from dotenv import load_dotenv
import os

load_dotenv()                       # read the .env file
key = os.environ.get("API_KEY")
print(key)

One critical rule: never share your .env file. Add it to .gitignore so it's never uploaded to GitHub. Share an .env.example with blank values instead, so others know which settings they need to fill in.

You’re reading for free. Sign in to keep your progress and earn a certificate when you finish.Sign in to keep my progress →