【发布时间】:2017-08-19 16:53:23
【问题描述】:
我想从我的子模块导入一个类,而不必使用from submodule.submodule import Class 语法。相反,我只想像普通的 Python3 模块一样做from submodule import Class。
我觉得这个问题应该已经被回答了一百万次了,虽然关于 SO 有几个类似名称的问题,但没有一个通过简单的示例提供一个清晰、简单的解决方案。
我正在尝试使用此设置进行最简单的测试:
.
├── main.py
└── test
├── __init__.py
└── test.py
在我的test 模块中,我有以下内容:
test.py
class Test:
def __init__(self):
print('hello')
__init__.py
from test import Test
__all__ = ['Test']
在上层 main.py 我有以下内容:
from test import Test
Test()
当我尝试运行 main.py 时,我得到:
ImportError: cannot import name 'Test'
我知道我可以用from test.test import Test 替换main.py 中的导入语句,但我的理解是__init__.py 的要点之一是使子模块可以在包级别访问(__all__ 允许导入都是from test import *)
【问题讨论】:
标签: python-3.x