代码样式“展平”包的名称p

2024-09-30 14:21:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我的包层次结构:

InstrumentController/
    __init__.py
    instruments/
        __init__.py
        _BaseInstrument.py
        Keithley2000.py
        # etc...

仪器文件的内容:

^{pr2}$

我希望我的用户能够访问这些类,而不必深入研究模块的层次结构。他们只需要输入from InstrumentController.instruments import Keithley2000,而不是{}。在

为此,我在InstrumentController.instruments.__init__中有一堆类似这样的行:

from .Keithley2000 import Keithley2000
from .StanfordSR830 import StanfordSR830
# etc...

所以现在类位于包名称空间的顶部,而不是子模块中。我的问题是:这是个好主意吗?这些类与它们所属的模块具有相同的名称,因此在顶层导入类会导致模块不可用。这让我有点紧张-有更好的方法吗?在


Tags: 模块文件frompyimport名称层次结构init