4

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.

Community
  • 1
  • 1
Ankita Gupta
  • 155
  • 2
  • 14

2 Answers2

3
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')
Nikhil Rupanawar
  • 4,061
  • 10
  • 35
  • 51
  • So, If I change the name of the key, will this still work? In that case, I won't have to create another class. I want to avoid this because I have to call this config file in multiple other files. – Ankita Gupta Jan 27 '17 at 09:15
  • Probably a monkey patch `SafeConfigParser.optionxform = lambda self, st: st ` – Nikhil Rupanawar Jan 27 '17 at 09:24
  • I have accepted this answer but since I have less reputation, it isn't showing in the count. – Ankita Gupta Jan 27 '17 at 10:30
-2

You should remember that the default section should be capitalised.

[DEFAULT]

Making this change should solve the problem.

mic4ael
  • 7,974
  • 3
  • 29
  • 42