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.

2
  • 1
    have you tried using environment variable names that don't shadow the original key? Also you might want to see whether the tabs underneath [amazon] section matter. Commented Jan 27, 2017 at 8:50
  • There are no tabs under the [amazon] section. Commented Jan 27, 2017 at 9:19

2 Answers 2

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')
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Probably a monkey patch SafeConfigParser.optionxform = lambda self, st: st
I have accepted this answer but since I have less reputation, it isn't showing in the count.
-2

You should remember that the default section should be capitalised.

[DEFAULT]

Making this change should solve the problem.

1 Comment

changing to [DEFAULT] didn't help. I am still getting InterpolationMissingOptionError

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.