我仅使用 _
module1
创建了子文件夹
_init__.py
def print_hello():
print('Hello')
__all__ = ["print_hello"]
print('Init loaded')
里面
main.py
我有
import module1
print_hello()
输出如下
print_hello()
^^^^^^^^^^^
NameError: name 'print_hello' is not defined
Init loaded
导入模块不会自动将其所有变量和函数引入当前命名空间。
您仍然需要参考您要导入的具体内容。
import module1
module1.print_hello()
或
from module1 import print_hello()
或
from module1 import *