Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Python’s os.getenv method returns the value of the environment variable key, if it exists.

(For testing on your computer see Create Windows Environment Variables with PowerShell)

os.getenv Usage

Code Block
languagepy
import os

# Retrieve the value of the PASSWORD environment variable
password_credential = os.getenv('PASSWORD')
print(f"Password credential: {password_credential}")

# Optional Default if environment variable does not exist
# Attempt to retrieve a non-existent environment variable with a default value
custom_variable = os.getenv('CUSTOM_VARIABLE', 'default_value')
print(f"Custom Variable: {custom_variable}")

...