Created
December 3, 2021 08:42
-
-
Save Hugovdberg/e26410c64ef6419b41ab720b08b50da3 to your computer and use it in GitHub Desktop.
Check if multiple environment variables are present and list all missing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| MANDATORY_ENV_VARS = {'TEMP', 'USER'} | |
| # Python 3.8 or higher, using empty set is falsy: | |
| if missing_vars := MANDATORY_ENV_VARS.difference(os.environ): | |
| raise EnvironmentError(f"The following variables were not set: {missing_vars}") | |
| # Python 3.7 or lower: | |
| if not MANDATORY_ENV_VARS.issubset(os.environ): | |
| missing_vars = MANDATORY_ENV_VARS.difference(os.environ) | |
| raise EnvironmentError(f"The following variables were not set: {missing_vars}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment