Sub topic 5
Using a .env file for local development
This chapter assumes you can use a terminal, know basic Git, and can read short Python or JavaScript snippets. You have already seen how to set an environment variable in your shell. Here you will store the key in a file so you do not have to export it every time you open a terminal.
Why a .env file
An exported variable lasts only for the current shell session. Close the terminal and it is gone. A .env file is a plain text file in your project folder. Each line holds one KEY=value pair. A small library reads the file when your program starts and copies the pairs into the process environment.
Your code then reads the key the same way it would in production, from the environment. Only the source of the value changes. That keeps one code path for local work and for deployment.
Creating the file
Create a file named .env in the project root, next to your main script:
# .env
LLM_API_KEY=paste-your-key-hereFollow these format rules:
- Write one pair per line, with no spaces around
=. - Quotes are usually optional. Use them if the value contains spaces or a
#. - A line starting with
#is a comment. - Do not add a trailing comment on the same line as a value unless your loader documents that it supports it.
Loading it in code
The library python-dotenv reads the file and fills os.environ. Install it with pip install python-dotenv.
import os
from dotenv import load_dotenv # pip install python-dotenv
load_dotenv() # reads .env into os.environ; real environment variables are not overridden by default
api_key = os.environ.get("LLM_API_KEY")
if not api_key:
raise RuntimeError("LLM_API_KEY is not set")Two details matter here:
load_dotenv()does not override variables that already exist. IfLLM_API_KEYis exported in your shell, the shell value wins over the file. This is useful in CI, where the real environment should take priority. It can also confuse you when an old exported key keeps being used.- The check after loading makes a missing key fail at startup with a clear message. Without it, you would see a vague authentication error later.
Common errors
| Symptom | Likely cause |
|---|---|
| Key is `None` or "not set" | The program runs from a different folder than the one holding `.env`. Pass a path, or run from the project root. |
| Old key still used | A shell export overrides the file. Run `unset LLM_API_KEY` and try again. |
| Key has odd characters | Extra spaces or quotes were copied into the value. |
| Authentication fails with a correct key | A hidden newline or space at the end of the line. |
The file is a secret
A .env file holds a real credential in plain text. Treat it like a password file.
- Never commit it. The next chapter covers keeping it out of Git.
- Never paste its contents into chats, tickets, or screenshots.
- A common practice is to commit a
.env.examplefile that lists variable names with fake values, so teammates know what to set.
Exercise
You export LLMAPIKEY in your shell with an old key, then run a Python script that calls load_dotenv() with a new key in .env. Which key does the script use, and why?
Show answer
The old key from the shell. By default load_dotenv() does not override variables that are already set in the environment, so the exported value wins over the .env value.