如何将 ConfigParser 与 virtualenv 一起使用?

发布于 2024-09-28 14:59:37 字数 777 浏览 0 评论 0原文

我编写了一个工具,可以在多个位置查找 INI 配置文件:在 /usr/share/usr/local/share~/.local/ 中共享,并在当前目录中。

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 目录?

I wrote a tool that looks in several places for an INI config file: in /usr/share, /usr/local/share, ~/.local/share, and in the current directory.

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

I have started using virtualenv and now my setup.py script installs myconfig.conf into /path/to/virtual/env/share/. How can I add this path to the list of paths searched by ConfigParser when the path to the virtualenv will be different each time? Also, if I installed to a virtualenv, should I still search the system /usr/share and /usr/local/share directories?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼泪 2024-10-05 14:59:37

您应该能够通过

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

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

You should be able to get the venv share path with

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

Including /usr/share or /usr/local/share would depend on your application and if multiple installations by different users would be more likely to benefit or be harmed by global machine settings. Using the above code would include '/usr/share/myconfig.conf' when using the system python so it is probably safer to not include it explicitly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文