【发布时间】:2021-07-11 04:35:52
【问题描述】:
我正试图了解全局变量。我创建了一个名为 Test_Folder 的包,其中包含两个文件 - __init__.py 和 test_module.py。然后我有一个名为Test_File.py (在包外)的单独文件,我试图从中运行代码。
在我的 test_module 中我定义了一个函数:
def practice():
print("Hello", constant_one)
practice()
然后在我的__init__ file 中设置constant_one 并定义__all__ 的范围:
__all__ = ["test_module"]
constant_one = 23
最后——在我的 test_file 中导入包:
from Test_Folder import *
但是——当我运行这个导入时,我得到一个 NameError——没有定义 constant_one。
我理解这意味着练习函数正在运行,但我不确定为什么没有定义 constant_one?
【问题讨论】:
标签: import package global-variables