Skip to main content
Every real application needs configuration — API keys, database URLs, debug flags, port numbers. Hardcoding these values directly in your source code is a serious security risk: secrets become visible to anyone who reads the code, and they can accidentally end up on GitHub. The correct approach is to read configuration from environment variables, and the easiest way to manage those variables during development is a .env file.

Why Not Hardcode Configuration?

Problems with this approach:
  • Secrets are exposed to anyone who reads your code.
  • Values may accidentally be committed to a public repository.
  • Every developer on the team must edit source files to use different values.
  • Different environments (development, testing, production) require different settings.

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, and your Python code simply requests them by name:
Your code never needs to know where the value came from — it simply asks the OS.

Setting Variables Manually (macOS / Linux)

Variables set with export exist only for the current terminal session. Once the terminal closes, they disappear. Managing ten variables this way every time you open a terminal is error-prone and time-consuming.

What Is a .env File?

A .env file is a plain text file that stores environment variables so you only have to write them once:
Python cannot read .env files automatically. You need the python-dotenv package.

Setting Up python-dotenv

1

Install the package

2

Create your .env file

3

Load and read the variables

Call load_dotenv() once at the beginning of your application, before any other code reads from the environment.

Reading Variables Safely

Prefer os.environ.get() over os.environ["KEY"]. The get() form returns None instead of crashing when a variable is missing. You can also supply a fallback default:

Complete Example

.env
app.py

Never Commit .env to Git

Never commit .env files to version control!A .env file typically contains API keys, database passwords, secret keys, and access tokens. Always add it to .gitignore:

Sharing Projects Safely

Instead of sharing your real .env, create a template named .env.example with placeholder values and commit that instead:
Other developers copy the template and fill in their own values:


Best Practices

  • Use UPPERCASE names: DATABASE_URL, API_KEY, SECRET_KEY
  • One variable per line
  • No spaces around =: write PORT=8000, not PORT = 8000
  • Use # comments to group related variables
  1. Call load_dotenv() at the very start 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 all configuration outside your source files.
Using .env files is a standard practice in modern Python development — you’ll find it in FastAPI, Flask, Django, and virtually every other framework.