从另一个导入子模块

时间:2014-06-21 19:18:52

标签: python module

我虽然很容易,但我无法做到。 我在the doc中读到这应该有效:

档案a/__init__.py

 #nothing here

档案a/b.py

var = "hello world"

档案a/c.py

import b
print(b.var)

来自.

>>> import a.c
ImportError: No module named 'b'

但它没有......我忘记了什么?是因为我使用Python 3吗?

2 个答案:

答案 0 :(得分:1)

是的,这是因为Python3。

In Python 3, implicit relative imports within packages are no longer available

Changes in import statement python3

答案 1 :(得分:0)

没关系,我相信你应该把你的文件保存在Program Files(x86)/ Lib中来创建一个新模块,就像Python 2.7.6一样,但它很可能也适用于Python 3.0。