0

我编写了一个工具,它在多个位置查找 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目录吗?

4

1 回答 1

1

您应该能够获得 venv 共享路径

os.path.join(sys.prefix, 'share', 'myconfig.conf')

包括/usr/share/usr/local/share将取决于您的应用程序,以及不同用户的多次安装是否更有可能受益或受到全局机器设置的损害。在使用系统 python 时,使用上面的代码会包含在内'/usr/share/myconfig.conf',因此不显式包含它可能更安全。

于 2010-10-21T14:51:20.763 回答