【发布时间】:2021-02-05 10:42:57
【问题描述】:
我有一个简单的包结构,其中包含一个名为 ui 的模块:
test/
├── app.py
├── __init__.py
└── ui
├── __init__.py
└── window.py
1 directory, 4 files
window.py 文件包含一个基本类:
# test/ui/window.py
class Window():
def __init__(self):
print("Window constructor")
在我的app.py 我有:
# test/app.py
from ui import window
def run():
w = window.Window()
现在在 Python 3 shell 中,我应该可以从包 test 中导入模块 app,像这样调用 run 函数(我在包的父目录中):
>>> import test.app
>>> test.app.run()
但是我得到了这个错误(使用 Python3):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test/app.py", line 1, in <module>
from ui import window
ModuleNotFoundError: No module named 'ui'
请注意,这适用于 Python2.7...
这里有什么问题?
【问题讨论】:
标签: python python-3.x python-2.7 module package