扩展插值在 configparser 中不起作用

Extended interpolation not working in configparser

我尝试在 python 3.6 或 python 3.5.1

中使用标准库中的 configparser 模块

我的 ini 文件如下所示:

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

[HUE_310]
partNewFilePath = ${common:domain}

我的 "main" 程序如下所示:

from configparser import ConfigParser

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

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

相反 http://some_domain_name:8888 我得到了 ${common:domain}

我使用 Pycharm 社区作为我的 IDE。我使用 virtualenv.

我不知道我的代码有什么问题...

如果你想要扩展插值,你必须通过调用configparser.ExtendedInterpolationclass、创建一个实例,然后在创建 ConfigParser 实例时将其与 interpolation= 关键字参数一起使用,如下所示:

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