Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Centos Python 3:使用环境变量和交叉引用的ConfigParser_Python_Python 3.x_Configparser - Fatal编程技术网

Centos Python 3:使用环境变量和交叉引用的ConfigParser

Centos Python 3:使用环境变量和交叉引用的ConfigParser,python,python-3.x,configparser,Python,Python 3.x,Configparser,当我尝试使用ConfigParser获取值时 我无法从.ini文件中的环境变量中获取某些值 插值变量名是打印变量,因为它没有值替换 下面是我的代码 config\u parse.py: import os import configparser myConf = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation()) myConf.read('config_data.ini') pri

当我尝试使用ConfigParser获取值时

  • 我无法从.ini文件中的环境变量中获取某些值

  • 插值变量名是打印变量,因为它没有值替换

下面是我的代码

config\u parse.py:

import os
import configparser

myConf = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
myConf.read('config_data.ini')

print( myConf.get('server_details', 'hostName', vars=os.environ))
print( myConf.get('server_details', 'userName', vars=os.environ))
print( myConf.get('log_path', 'mntPath', vars=os.environ))
exit(0)
[server_details]
  hostName: %(HOSTNAME)
  ; Below value are not getting substituted from environment variable
  userName: %(USER)
  password: passw0rd

[log_path]
  instance: %(SERVER_INSTANCE)
  mntPath: /net/server1/mnt/data0
  ; server_details:hostname and instance values are not getting substituted
  testbedMntPath: ${mntPath}/${server_details:userName}/my_logs/${server_details:hostName}${instance}
config_data.ini:

import os
import configparser

myConf = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
myConf.read('config_data.ini')

print( myConf.get('server_details', 'hostName', vars=os.environ))
print( myConf.get('server_details', 'userName', vars=os.environ))
print( myConf.get('log_path', 'mntPath', vars=os.environ))
exit(0)
[server_details]
  hostName: %(HOSTNAME)
  ; Below value are not getting substituted from environment variable
  userName: %(USER)
  password: passw0rd

[log_path]
  instance: %(SERVER_INSTANCE)
  mntPath: /net/server1/mnt/data0
  ; server_details:hostname and instance values are not getting substituted
  testbedMntPath: ${mntPath}/${server_details:userName}/my_logs/${server_details:hostName}${instance}
我得到以下输出

$]python config_parse.py

服务器1

%(用户)

/net/server1/mnt/data0/%(用户)/我的日志/%(主机名)%(服务器实例)

$]

您正在使用,它使用
${section:option}
语法

之所以为myConf.get('server\u details','hostName',vars=os.environ)获取
server1
,不是因为
hostName=%(hostName)
是插值的,而是因为
hostName
来自
vars

用一个例子更容易看出:

>>> myConf.get('server_details', 'foo', vars={'FOO': 'BAR'})
'BAR'
如果
vars
包含
FOO
的条目,则
get
调用
FOO
(不区分大小写)将返回其值,实际上忽略配置文件中的值

这意味着对于
hostName
您可以实际获得
hostName
环境变量的值
userName
os.environ
中不存在,因此您可以从配置文件中获取值:
%(USER)


因此,如果使用扩展插值,请使用
${…}
样式,如果使用基本插值,请使用
%(…)s
样式。你不能两者兼而有之。如果您盲目地传递
vars=os.environ
,请注意,环境变量将覆盖具有相同名称的所有部分中的所有选项。根据应用程序运行的上下文,这可能与安全性有关。

但如果我打印os.environ值,则说明已正确定义了
USER
变量。为什么我不能在我的
[server\u details]
部分中看到该值?因为扩展插值
%(HOME)
并不特别,它只是一个字符串(基本插值应该是
%(HOME)s
)。os.environ是否有一个键
USER
,这并不重要,它没有被使用。如果环境变量
USERNAME
您将得到它,因为查找总是首先在
vars
中执行。没有一个
%(…)
选项使用您案例中环境中的任何内容。您使用扩展插值,因此将其更改为
${USER}
,这样它就可以工作了。