Why os.getenv() Ignores Your .env File (and How to Fix It)
If you’ve ever logged an environment variable in Python and thought, “Why is this value coming from my system instead of my .env file?” — you’re not alone.
This is a very common source of confusion when working with AWS credentials, profiles, or configuration-driven applications.
Let’s break down why this happens, how environment variable precedence works, and how to make your .env file behave the way you expect.
The Core Issue: .env Files Are Not Automatic
Calling:
os.getenv(“AWS_PROFILE”)
does not read your .env file by default.
Python only knows about variables that already exist in the process environment. A .env file is just a text file until you explicitly load it.
That’s why libraries like python-dotenv exist.
Loading the .env File Correctly
To load values from a .env file into the environment, you must do this explicitly and early:
from dotenv import load_dotenv
load_dotenv()
After this runs, variables defined in .env become available via os.getenv().
However, this alone does not guarantee your .env values will be used.
Environment Variable Precedence (The Real Gotcha)
Even when load_dotenv() is working correctly, system-level environment variables always take precedence.
If a variable exists in both places:
- System environment (PowerShell, shell, OS)
- .env file
Python will use the system value, not the .env value.
This is intentional behavior.
Forcing .env to Override System Variables
If you want the .env file to override existing environment variables, you must opt in:
load_dotenv(override=True)
Without override=True, python-dotenv will not replace values that already exist in the environment.
Verifying Where the Value Is Coming From
To debug what’s happening, it helps to inspect both sources:
from dotenv import load_dotenv, dotenv_values
import os
load_dotenv(override=False)
print(“Value in .env:”, dotenv_values().get(“AWS_PROFILE”))
print(“Value in environment:”, os.getenv(“AWS_PROFILE”))
This makes it immediately clear whether:
- the .env file was loaded
- the system environment is overriding it
Common Causes of .env Being Ignored
- The variable is already set in your shell
On Windows (PowerShell):
echo $Env:AWS_PROFILE
If this prints a value, it will override .env unless override=True is used.
- The .env file isn’t in the working directory
load_dotenv() searches relative to the current working directory. If your script runs from a subfolder, the file may not be found.
You can confirm this with:
import os
print(os.getcwd())
Or specify the file explicitly:
load_dotenv(dotenv_path=”/path/to/.env”, override=True)
- The .env syntax is invalid
The .env file must use simple KEY=value syntax.
Correct:
AWS_PROFILE=dev
Incorrect:
AWS_PROFILE = “dev”
- .env is loaded too late
Always call load_dotenv() before importing modules that read environment variables.
Key Takeaways
- getenv() only reads environment variables, not .env files
- .env files must be explicitly loaded
- System environment variables override .env by default
- Use override=True if you want .env to win
- Always verify where values are coming from when debugging configuration issues
Understanding these rules will save you hours of frustration—especially when working with AWS profiles, credentials, and multi-environment setups.

