Skip to main content

Why do we need environment variables?

Imagine you have a Python application that needs:
  • An API key for OpenAI
  • A database connection string
  • A secret key for authentication
  • A port number
  • A debug mode flag
You should not hardcode these values directly in your source code. ❌ Bad:
Why is this bad?
  • Secrets become visible in your source code.
  • They may accidentally be committed to GitHub.
  • Every developer needs to edit the source code.
  • Different environments (development, testing, production) require different values.
Instead, Python applications read these values from environment variables.

What are environment variables?

Environment variables are key-value pairs maintained by the operating system. They are available to every program running in that environment. Think of them as configuration values provided from outside your application.
Instead of storing configuration inside your code, your application simply asks the operating system for the value.
Your code never knows where the value came from—it simply requests it.

Creating environment variables manually

macOS / Linux

Now Python can access them.
These variables exist only for the current terminal session. Once the terminal is closed, they disappear.

Viewing environment variables

List all variables:
or
View a specific variable:

The problem with manual environment variables

Although export works, it becomes difficult to manage. Imagine setting ten variables every time you open a terminal.
Problems include:
  • Easy to forget one variable
  • Time-consuming
  • Difficult for teammates
  • Different values for different projects
A better solution is using a .env file.

What is a .env file?

A .env file is simply a text file that stores environment variables. Instead of typing multiple export commands, you write them once. Example:
The file is easy to edit, easy to maintain, and easy to share as a template.

How does Python read a .env file?

Python cannot read .env files automatically. We use the python-dotenv package.

Install

Loading the .env file

Once loaded, every value behaves exactly like a normal environment variable.

Accessing variables safely

Instead of
prefer
You can even provide a default value.
This prevents your application from crashing if the variable is missing.

Project structure

The .env file normally lives in the project root.

Complete example

.env
app.py

Critical: Never commit .env

Never commit .env files to Git!A .env file usually contains:
  • API keys
  • Database passwords
  • Secret keys
  • Access tokens
Add .env to .gitignore.

Sharing projects safely

Instead of sharing your real .env, create a template named .env.example.
Other developers can copy it.
Then replace the placeholder values with their own.

Best practices

✅ Use UPPERCASE names
✅ One variable per line
✅ No spaces around =
❌ Avoid
✅ Use comments

Common environment variables

Complete workflow

Quick tips

  1. Call load_dotenv() at the beginning of your application.
  2. Use os.environ.get() instead of hardcoding values.
  3. Never commit .env to GitHub.
  4. Share .env.example instead.
  5. Keep configuration outside your source code.

Summary

Environment variables provide configuration outside your application, making your code more secure, flexible, and portable. A .env file is a convenient way to store these variables during development, and the python-dotenv package loads them automatically into your application’s environment. Using .env files is a standard practice in modern Python development, including FastAPI, Flask, Django, and many other frameworks.