Python Practice › OS Module Programs › Program 2
Read an environment variable
Write a Python program to read an environment variable.
Output
True development
Approach
os.environ.get(name, default)oros.getenv(name, default).
Solution
import os print("PATH" in os.environ)print(os.getenv("APP_MODE", "development"))Output
True development
Line by line
Line 4
APP_MODE is not set, so the default is returned instead of an error.
Learn the concept
New to this topic? Read the lesson, then come back.