1

I am getting the following error--- "configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%dCUD'"

I want to read credentials which are set as an environment variables as well as present in .config file. So I am using pythons ConfigParser as follows.

import configparser as cp
from configparser import ConfigParser
config = ConfigParser(os.environ)    #<-- this enables ConfigParser to read from environment variable
config.read(CONFIG_FILEPATH)       #<--- this is to read from .confog file


My .config file is like this--
[Postgres]
Postgres.host = XXX.com
Postgres.METADATADB=pda-study-beta
Postgres.DATAREFRESHDB=
Postgres.user= %(XXXX)s
Postgres.password = %(Postgres_Pass)s

anything included inside *%()s* means it is being read from environment variable. 

I read it in my scripts as follows:
config.get('Postgres','Postgres.password')


It works fine for all but for password, its throwing me following error in password section--
"configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%dCUD'"

Its because my password contains '%' character. e.g.(xxx%dCUDxx)

Does any one have any idea how do I handle this. We can escape % with another %, but in my case, I am reading password from environment variable, so cant manipulate it. 

Can anyone please help me resolve this ?

1 Answer 1

1

The configparser module documentation is ambiguous but if you look at the source code you can see, in method _interpolate_some, that % expansion is recursive. That is, % expansion is also done to the interpolated value -- the password in your example.

I am reading password from environment variable, so cant manipulate it.

It's true you probably don't want to mutate the global env vars. But nothing stops you from making a copy and mutating that copy. For example,

config = ConfigParser({k: v.replace('%', '%%') for k, v in os.environ.items()})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Kurtis Rader.

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.