繁体   English   中英

如何在Virtualenv中使用ConfigParser?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM