Skip to content

Instantly share code, notes, and snippets.

@FerusAndBeyond
Last active May 2, 2023 09:28
Show Gist options
  • Save FerusAndBeyond/952394b650befaf51d6a8471e9f4bd63 to your computer and use it in GitHub Desktop.
Save FerusAndBeyond/952394b650befaf51d6a8471e9f4bd63 to your computer and use it in GitHub Desktop.
env-variable part 1
import os
# first way
os.environ['TERM_PROGRAM']
# => 'Apple_Terminal'
# a non-existing variable will lead to error
os.environ["THIS_VARIABLE_DOESNT_EXIST"]
# => KeyError: 'THIS_VARIABLE_DOESNT_EXIST'
# second way
print(os.getenv("TERM_PROGRAM"))
# => 'Apple_Terminal'
# a non-existing variable will lead to None
print(os.getenv("THIS_VARIABLE_DOESNT_EXIST"))
# => None
# but you could also specify if you want it to return
# something else if the key does not exist
print(os.getenv("THIS_VARIABLE_DOESNT_EXIST", "Return this if it does not exist..."))
# => "Return this if it does not exist..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment