python单个配置文件
我正在开发一个项目,需要一个配置文件,其数据由多个模块使用。
我的问题是:常见的方法是什么?我应该从每个中读取配置文件吗 我的模块(文件)的或者有其他方法可以做到这一点吗?
我正在考虑有一个名为 config.py 的模块来读取配置文件,每当我需要配置时,我都会 import config
,然后执行类似 config.data['teamsdir']< /code> 获取
'teamsdir'
属性(例如)。
响应:选择conf.py方法,因为它是模块化的,灵活且简单的,
我可以将配置数据直接放入文件中,如果我想从json文件中读取xml,则可以将配置数据直接放入文件中文件或多个源我只需更改conf.py并确保以相同的方式访问数据。
接受的答案:选择“Alex Martelli”回复,因为它是最完整的。投票赞成其他答案,因为它们也很好而且有用。
I am developing a project that requires a single configuration file whose data is used by multiple modules.
My question is: what is the common approach to that? should i read the configuration file from each
of my modules (files) or is there any other way to do it?
I was thinking to have a module named config.py that reads the configuration files and whenever I need a config I do import config
and then do something like config.data['teamsdir']
get the 'teamsdir'
property (for example).
response: opted for the conf.py approach then since it it is modular, flexible and simple
I can just put the configuration data directly in the file, latter if i want to read from a json file a xml file or multiple sources i just change the conf.py and make sure the data is accessed the same way.
accepted answer: chose "Alex Martelli" response because it was the most complete. voted up other answers because they where good and useful too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我喜欢单个 config.py 模块的方法,其主体(首次导入时)解析一个或多个配置数据文件并适当地设置其自己的“全局变量” - 尽管我更喜欢 < code>config.teamdata 优于迂回的
config.data['teamdata']
方法。这假设配置设置在加载后是只读的(除了在单元测试场景中,测试代码将自己对
config
变量进行人工设置,以正确执行被测代码)——它基本上利用了模块的本质作为最简单的 Pythonic 形式的“单例”(当然,当你不需要子类化或其他仅由类而不是模块支持的功能时)。“一个或多个”配置文件(例如,第一个位于
/etc
中用于一般默认设置,然后位于/usr/local
中用于特定于站点的覆盖,然后再次可能在用户的主目录中用于用户特定的设置)是一种常见且有用的模式。I like the approach of a single
config.py
module whose body (when first imported) parses one or more configuration-data files and sets its own "global variables" appropriately -- though I'd favorconfig.teamdata
over the round-aboutconfig.data['teamdata']
approach.This assumes configuration settings are read-only once loaded (except maybe in unit-testing scenarios, where the test-code will be doing its own artificial setting of
config
variables to properly exercise the code-under-test) -- it basically exploits the nature of a module as the simplest Pythonic form of "singleton" (when you don't need subclassing or other features supported only by classes and not by modules, of course)."One or more" configuration files (e.g. first one somewhere in
/etc
for general default settings, then one under/usr/local
for site-specific overrides thereof, then again possibly one in the user's home directory for user specific settings) is a common and useful pattern.你描述的方法是可以的。如果您想添加对用户配置文件的支持,可以使用 execfile(os.path.expanduser("~/.yourprogram/config.py")) 。
The approach you describe is ok. If you want to add support for user config files, you can use
execfile(os.path.expanduser("~/.yourprogram/config.py"))
.一种不错的方法是在应用程序启动时将配置文件解析为 Python 对象,并将该对象传递给需要访问配置的所有类和模块。
这可能会节省大量解析配置的时间。
One nice approach is to parse the config file(s) into a Python object when the application starts and pass this object around to all classes and modules requiring access to the configuration.
This may save a lot of time parsing the config.
如果您想在不同的机器上共享您的配置,您也许可以将其放在 Web 服务器上并像这样导入:
如果您想在不同的语言之间共享它,也许您可以使用 JSON 来编码和解析配置:
If you want to share your config across different machines, you could perhaps put it on a web server and do import like this:
And if you want to share it across different languages, perhaps you can use JSON to encode and parse the configuration: