【发布时间】:2014-05-02 10:14:13
【问题描述】:
如何使用来自同级包的模块的绝对导入?
包文件结构:
.
├── a
│ ├── __init__.py
│ └── modulea.py
├── b
│ ├── __init__.py
│ ├── moduleb.py
├── __init__.py
└── test.py
文件 test.py 和 a/modulea.py:
from b.moduleb import f
if __name__ == '__main__':
f()
文件 b/moduleb.py:
def f():
print('hello')
这行得通:
% python test.py
hello
这不是:
% python a/modulea.py
Traceback (most recent call last):
File "a/modulea.py", line 1, in <module>
from b.moduleb import f
ImportError: No module named 'b'
据我所知,它应该可以工作:http://docs.python.org/3.3/tutorial/modules.html#intra-package-references。我错过了什么吗?
【问题讨论】:
标签: python python-3.x python-import