代码之家  ›  专栏  ›  技术社区  ›  Nathan Farrington

如何在virtualenv中使用configparser?

  •  0
  • Nathan Farrington  · 技术社区  · 15 年前

    我编写了一个工具,可以在多个地方查找in i配置文件:in /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/本地/共享 目录?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Marc    15 年前

    你应该能够得到与之共享的Venv路径

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

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