如何将 ConfigParser 与 virtualenv 一起使用?
我编写了一个工具,可以在多个位置查找 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够通过
包括
/usr/share
或/usr/local/share
来获取 venv 共享路径,具体取决于您的应用程序以及不同用户的多个安装是否会更有可能受益或受到全球机器设置的损害。使用系统 python 时,使用上面的代码将包含'/usr/share/myconfig.conf'
,因此不显式包含它可能更安全。You should be able to get the venv share path with
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.