在 python 中多次从外部文件调用变量
Calling variable from external file multiple times in python
我正在尝试从外部文件调用变量。为此,我写了这段代码,
count = 1
while (count <= 3):
# I want to iterate this line
# rand_gen is the python file
# A is the varialbe in rand_gen.py
# Having this expression A = np.random.randint(1, 100)
from rand_gen import A
print('Random number is ' + str(A))
count = count + 1
但是当我 运行 我的代码时,它只调用一次变量 A
并打印相同的结果。查看代码的输出,
Random number is 48
Random number is 48
Random number is 48
如何在每次进入循环时从文件 rand_gen.py
调用变量 A
并更新值?请帮忙。
如果您为变量分配一个随机值,无论该值是如何获得的,引用该变量都不会改变该值。
a = np.random.randint(1, 100)
a # 12
# Wait a little
a # still 12
同样,当你导入你的模块时,模块代码被执行并且一个值被分配给A
。除非使用 importlib.reload
重新加载模块或您再次调用 np.random.randint
,否则 A
没有理由更改值。
您可能想要 A
一个函数,该函数 returns 是所需范围内的随机值。
# In the rand_gen module
def A():
return np.random.randint(1, 100)
这不是 import
在 python 中的工作方式。导入后,module
缓存在 sys.modules
中作为 key
、value
对模块名称和模块对象。当您尝试再次导入相同的 module
时,您只需取回已缓存的值。但是 sys.modules
是可写的,删除 the
键会导致 python
检查模块并再次加载。
虽然 Olivier 的回答是解决这个问题的正确方法,但为了您对 import
的理解,您可以试试这个:
import sys # Import sys module
count = 1
while (count <= 3):
# I want to iterate this line
# rand_gen is the python file
# A is the varialbe in rand_gen.py
# Having this expression A = np.random.randint(1, 100)
if 'rand_gen' in sys.modules: # Check if "rand_gen" is cached
sys.modules.pop('my_rand') # If yes, remove it
from my_rand import A # Import now
print('Random number is ' + str(A))
count = count + 1
输出
Random number is 6754
Random number is 963
Random number is 8825
建议阅读 The import system and The module cache 上的官方 Python 文档,以获得透彻的理解。
我正在尝试从外部文件调用变量。为此,我写了这段代码,
count = 1
while (count <= 3):
# I want to iterate this line
# rand_gen is the python file
# A is the varialbe in rand_gen.py
# Having this expression A = np.random.randint(1, 100)
from rand_gen import A
print('Random number is ' + str(A))
count = count + 1
但是当我 运行 我的代码时,它只调用一次变量 A
并打印相同的结果。查看代码的输出,
Random number is 48
Random number is 48
Random number is 48
如何在每次进入循环时从文件 rand_gen.py
调用变量 A
并更新值?请帮忙。
如果您为变量分配一个随机值,无论该值是如何获得的,引用该变量都不会改变该值。
a = np.random.randint(1, 100)
a # 12
# Wait a little
a # still 12
同样,当你导入你的模块时,模块代码被执行并且一个值被分配给A
。除非使用 importlib.reload
重新加载模块或您再次调用 np.random.randint
,否则 A
没有理由更改值。
您可能想要 A
一个函数,该函数 returns 是所需范围内的随机值。
# In the rand_gen module
def A():
return np.random.randint(1, 100)
这不是 import
在 python 中的工作方式。导入后,module
缓存在 sys.modules
中作为 key
、value
对模块名称和模块对象。当您尝试再次导入相同的 module
时,您只需取回已缓存的值。但是 sys.modules
是可写的,删除 the
键会导致 python
检查模块并再次加载。
虽然 Olivier 的回答是解决这个问题的正确方法,但为了您对 import
的理解,您可以试试这个:
import sys # Import sys module
count = 1
while (count <= 3):
# I want to iterate this line
# rand_gen is the python file
# A is the varialbe in rand_gen.py
# Having this expression A = np.random.randint(1, 100)
if 'rand_gen' in sys.modules: # Check if "rand_gen" is cached
sys.modules.pop('my_rand') # If yes, remove it
from my_rand import A # Import now
print('Random number is ' + str(A))
count = count + 1
输出
Random number is 6754
Random number is 963
Random number is 8825
建议阅读 The import system and The module cache 上的官方 Python 文档,以获得透彻的理解。