Zest.

Python: Read an environment variable

A short, tested Python example for read an environment variable. Copy it and adjust the names and paths for your project.

Python
import os

key = os.environ.get("API_KEY")          # None if missing
port = int(os.getenv("PORT", "8000"))     # with a default
secret = os.environ["SECRET"]             # KeyError if missing

Find it in Zest

Search for “python read an environment variable” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.

More Python examples

All Python examples