Python Config Parser环境变量
发布时间:2020-12-20 13:15:52 所属栏目:Python 来源:网络整理
导读:我试图将一些环境变量读入我的ConfigParser文件. 我指的是this answer,但我得到了 "InterpolationDepthError: Value interpolation too deeply recursive" error.section: [amazon] option : amazon_access_key rawval : %(AMAZON_ACCESS_KEY)s 这是我的配置
我试图将一些环境变量读入我的ConfigParser文件.
我指的是this answer,但我得到了 "InterpolationDepthError: Value interpolation too deeply recursive" error. section: [amazon] option : amazon_access_key rawval : %(AMAZON_ACCESS_KEY)s 这是我的配置文件的一部分: [amazon] amazon_access_key=%(AMAZON_ACCESS_KEY)s amazon_secret_key=%(AMAZON_SECRET_KEY)s 这就是我要写的文件: from ConfigParser import SafeConfigParser import os config = SafeConfigParser(os.environ) config.read('config.txt') 当我直接在“亚马逊”部分调用这些变量时,我得到上述错误. 当我在“默认”部分中调用这些变量时,例如: [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 我收到以下错误: ConfigParser.InterpolationMissingOptionError: Bad value substitution: section: [amazon] option : amazon_access_key key : aws_access_key rawval : %(aws_access_key)s 我在这里错过了什么? 另外,如何为本地和生产部署提供单独的配置文件?目前,本地和生产环境的所有配置都相同. 解决方法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') 说明: [amazon] amazon_access_key=%(amazon_access_key)s 另一种解决方案是 – 在config.txt中更改密钥的名称,不等于env vars. [amazon] aak=%(AMAZON_ACCESS_KEY)s 然后 config = SafeConfigParser(os.environ) config.read('config.ini') print config.get('amazon','aak') (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |