为什么从。导入在这种情况下不起作用?

时间:2019-08-20 12:53:55

标签: python python-3.x

我试图理解python(python3)中的相对导入,以下简单的代码段不起作用。我想知道我对相对进口的了解中缺少什么。

我有这样的目录结构:

example/
   a.py
   b.py

a.py具有以下代码:

def test_print():
   print("test")

和b.py具有以下代码:

from . import a

a.test_print()

当我运行b.py时,我得到一个ImportError: cannot import name 'a'。但是,如果我上一层(cd ..,然后运行python -m example.b,它将运行而没有任何错误。

0 个答案:

没有答案