heniekk
heniekk

Reputation: 414

Extended interpolation not working in configparser

I tried to use configparser module from standard library in python 3.6 or python 3.5.1

My ini file looks like this:

[common]
domain = http://some_domain_name:8888
about = about/
loginPath = /accounts/login/?next=/home
fileBrowserLink = /filebrowser
partNewDirName = some_dir

[HUE_310]
partNewFilePath = ${common:domain}

My "main" program looks like this:

from configparser import ConfigParser

parser = ConfigParser()
parser.read('configfile.ini')

lll = parser.get('HUE_310', 'partNewFilePath')
print(lll)

Instead http://some_domain_name:8888 I got ${common:domain}

I use Pycharm Community as my IDE. I use virtualenv.

I have no idea what is wrong with my code...

Upvotes: 10

Views: 7655

Answers (1)

martineau
martineau

Reputation: 123501

If you want extended interpolation, you have to create an instance of the configparser.ExtendedInterpolation class, by calling it, and then using that with the interpolation= keyword argument when you create the ConfigParser instance as shown below:

from configparser import ConfigParser, ExtendedInterpolation

parser = ConfigParser(interpolation=ExtendedInterpolation())
parser.read('configfile.ini')

lll = parser.get('HUE_310', 'partNewFilePath')
print(lll)  # -> http://some_domain_name:8888

Upvotes: 27

Related Questions