[英]How to use ConfigParser with virtualenv?
我编写了一个工具,该工具在多个位置查找INI配置文件: /usr/share
, /usr/local/share
, ~/.local/share
和当前目录中。
c = ConfigParser.RawConfigParser()
filenames = ['/usr/share/myconfig.conf',
'/usr/local/share/myconfig.conf',
os.path.expanduser('~/.local/share/myconfig.conf'),
os.path.expanduser('./myconfig.conf')]
parsed_names = c.read(filenames)
for name in parsed_names:
print 'using configuration file: ' + name
我已经开始使用virtualenv,现在我的setup.py
脚本将myconfig.conf
安装到/path/to/virtual/env/share/
。 当virtualenv的路径每次都不相同时,如何将该路径添加到ConfigParser搜索的路径列表中? 另外,如果我安装了virtualenv,是否仍应搜索系统/usr/share
和/usr/local/share
目录?
您应该能够获得venv共享路径
os.path.join(sys.prefix, 'share', 'myconfig.conf')
包括/usr/share
或/usr/local/share
取决于您的应用程序,以及不同用户的多次安装是否更可能受益于全局计算机设置或受到全局计算机设置的损害。 在使用系统python时,使用上述代码将包含'/usr/share/myconfig.conf'
,因此不明确包含它可能更安全。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.