读取conf文件并使内容可供包中的所有文件使用

发布于 2025-01-06 09:37:01 字数 312 浏览 4 评论 0原文

myMain.py
/myPackage
    file1.py
    file2.py
    confFile.cfg

myMain.py 导入 file1 和 file2 等并读取 confFile。 使conf选项可见的最佳方式是什么 整个包裹?即 file1 和 file2 能够读取 conf 选项。我不想传递很多变量 在函数中。

我现在拥有的是一个confFile.py,我将其导入到文件中 并根据需要读取从中定义的变量。

我是 python 新手,如有任何建议,我们将不胜感激。

/扎尔

myMain.py
/myPackage
    file1.py
    file2.py
    confFile.cfg

myMain.py imports file1 and file2 etc. and it reads the confFile.
what would be the best way for the conf options to be visible in
the whole package? i.e. for file1 and file2 to be able to read
the conf options. I do not want to pass a lot of variables around
in functions.

What I have right now is a confFile.py which i import into the files
and read the variables defined from it as they are required.

I am new to python and any suggestions are appreciated.

/Zaar

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

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

发布评论

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

评论(1

两人的回忆 2025-01-13 09:37:01

我认为最好的方法是只拥有一个 python 模块 config.py 或 settings.py 并将这些设置直接存储在这些 python 文件中而不是 confFile 中。 cfg。

然后你就可以通过以下方式获得你想要的任何东西:

import config
config.my_specific_option

I think the best way is to just have a python module config.py or settings.py and store those settings directly in those python files instead of the confFile.cfg.

Then you could just get anything you want by:

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