How can I access the value of an environment variable in Python?

Using os.getenv(): I’ve found os.getenv() to be a more streamlined way to fetch environment variables, especially when you want a cleaner syntax for setting a default value.

import os
value = os.getenv('MY_ENV_VAR', 'default_value')  # 'default_value' if the variable is not set
print(value)

It’s like os.environ but with a slightly more intuitive feel for many use cases. This exact match keyword: ‘os.getenv’ is great when you prefer code that reads naturally!