重构 python 模块配置以避免相对导入

2024-01-10

这与一个上一个问题 https://stackoverflow.com/questions/343517/how-do-i-work-with-multiple-git-branches-of-a-python-module我的。

我了解如何存储和读取配置文件。有这样的选择配置解析器 http://www.python.org/doc/2.5.2/lib/module-ConfigParser.html and 配置对象 http://www.voidspace.org.uk/python/configobj.html.

考虑假设的“eggs”模块的结构:



eggs/
  common/
    __init__.py
    config.py
  foo/
    __init__.py
    a.py
  

“eggs.foo.a”需要一些配置信息。我目前正在做的是,在“a”中,


import eggs.common.config  
. One problem with this is that if 'a' is moved to a deeper level in the module tree, the relative imports break. Absolute imports don't, but they require your module to be on your PYTHONPATH.

上述绝对导入的可能替代方案是相对导入。因此,在“a”中,


import .common.config  

在不讨论相对进口与绝对进口的优点的情况下,我想知道其他可能的解决方案?

编辑-删除了 VCS 上下文


“导入...要求您的模块位于 PYTHONPATH 上”

Right.

那么,设置有什么问题PYTHONPATH?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

重构 python 模块配置以避免相对导入 的相关文章

随机推荐