9

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...

martineau
  • 119,623
  • 25
  • 170
  • 301
heniekk
  • 374
  • 2
  • 10

1 Answers1

22

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
martineau
  • 119,623
  • 25
  • 170
  • 301
  • It worked. Thanks. When I use this I lost my basic interpolation so to stil get basic and extended interpolation I use something like this `basic = ConfigParser(interpolation=BasicInterpolation()) basic.read('config.ini')`. Is there a better way to achieve this? – heniekk Mar 16 '17 at 21:19
  • Not that I know of. You need to use the syntax defined by the `interpolation` class being used. You might be able to create your own class that handled both—you can peek at the source of the two provided to get an idea of how to do it. – martineau Mar 16 '17 at 21:20
  • 1
    Also note you can use extended syntax to do reference thing in the same section like `testowy = ${domain}s/${about}s` if `domain` and `about` are in the same section—so it can do "basic" stuff, too. – martineau Mar 16 '17 at 21:32
  • Yes, I had basic interpolation and it worked default. But when I use extended the basic stopped working, so I make this "dirty" hack from my first comment here. Also [configobj](http://configobj.readthedocs.io/en/latest/configobj.html) looks very promising. Thanks for this tip! – heniekk Mar 16 '17 at 21:37
  • 1
    No sure you understood my last comment. By changing the `%[` to `${`, the `ExtendedInterpolation` class can do _both_ extended and basic interpolation depending on whether there's a `:` in the referenced name. – martineau Mar 16 '17 at 21:57
  • 2
    Thanks for pointing this out. Now I understand. I don't need my dirty hack :) – heniekk Mar 16 '17 at 23:23
  • 2
    OMG it's an option you have to set when making the object. I was scratching my head why it wouldn't work. Why didn't thy put this under the dedicated section in the docs. – Swedgin May 15 '20 at 09:24
  • Wouldn't it be nice if the standard documentation (https://docs.python.org/3.9/library/configparser.html) provided this helpful information somewhere near its lovely section about "Extended Interpolation". – Tom Stambaugh Feb 17 '22 at 16:46