Python 调用 __init__.py 中定义的函数不起作用

问题描述 投票:0回答:1

我仅使用 _

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
python module init
1个回答
0
投票

导入模块不会自动将其所有变量和函数引入当前命名空间。

您仍然需要参考您要导入的具体内容。

import module1
module1.print_hello()

from module1 import print_hello()

from module1 import *
© www.soinside.com 2019 - 2024. All rights reserved.