I am trying to read some environment variables into my ConfigParser file.
I was referring to this answer but I am getting
"InterpolationDepthError: Value interpolation too deeply recursive" error.
section: [amazon]
option : amazon_access_key
rawval : %(AMAZON_ACCESS_KEY)s
Here is the portion of my config file:
[amazon]
amazon_access_key=%(AMAZON_ACCESS_KEY)s
amazon_secret_key=%(AMAZON_SECRET_KEY)s
And this is what I am writing to call the file:
from ConfigParser import SafeConfigParser
import os
config = SafeConfigParser(os.environ)
config.read('config.txt')
When I am calling these variables directly in the "amazon" section, I get the above stated error.
When I call these variables in the "default" section, like:
[default]
aws_access_key=%(AMAZON_ACCESS_KEY)s
aws_secret_key=%(AMAZON_SECRET_KEY)s
[amazon]
amazon_access_key=%(aws_access_key)s
amazon_secret_key=%(aws_secret_key)s
I get the following error:
ConfigParser.InterpolationMissingOptionError: Bad value substitution:
section: [amazon]
option : amazon_access_key
key : aws_access_key
rawval : %(aws_access_key)s
What am I missing here?
Also, how can I have separate config files for local and production deployments? Currently, all the configurations are same for local and production environments.
from ConfigParser import SafeConfigParser
import ConfigParser
import os
class CaseConfigParser(SafeConfigParser):
def optionxform(self, optionstr):
return optionstr
config = CaseConfigParser(os.environ)
config.read('config.ini')
print config.get('amazon', 'amazon_access_key')
Explanation: The problem here is with optionxform, which turns all options to lower case by default. eventually, it will have key and value equal, similar to following.
[amazon]
amazon_access_key=%(amazon_access_key)s
Another solution can be - change names of keys in config.txt NOT equal to env vars.
[amazon]
aak=%(AMAZON_ACCESS_KEY)s
and then
config = SafeConfigParser(os.environ)
config.read('config.ini')
print config.get('amazon', 'aak')