简体   繁体   中英

Import submodule from another one

I though it was easy, but I just can't do it. I read in the doc that this should work :

File a/__init__.py

 #nothing here

File a/b.py

var = "hello world"

File a/c.py

import b
print(b.var)

From . :

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

But it does not… What have I forgotten ? Is it because I use Python 3 ?

yes, it's because of Python3.

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

Changes in import statement python3

没关系,我相信您应该将文件保存在Program Files(x86)/ Lib中以创建一个新模块,适用于Python 2.7.6,但是很有可能也适用于Python 3.0。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM