Last active
February 21, 2020 13:53
-
-
Save thoth-ky/580b7fc4f0b317339e4357bcac74e5ed to your computer and use it in GitHub Desktop.
This GIST shows how to use configparser to interpolate env variables within
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 configparser | |
import os | |
class CustomEnvInterpolation(configparser.BasicInterpolation): | |
"""Interpolation which expands environment variables in values.""" | |
def before_get(self, parser, section, option, value, defaults): | |
super().before_get(parser, section, option, value, defaults) | |
if not os.environ.get('MY_ENV'): | |
os.environ['MY_ENV'] = 'default' | |
return os.path.expandvars(value) | |
config = configparser.ConfigParser(interpolation=CustomEnvInterpolation()) | |
def load_config(): | |
config.read('settings.cfg') | |
load_config() | |
print(os.environ.get('MY_ENV')) | |
print(config['settings']['environ']) |
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
[settings] | |
name = kyalo | |
environ = ${MY_ENV} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment