我有以下问题,我将分享四个不同的 .py 文件以更好地解释自己。我正在运行来自 spyder(不是 jupyter)python 3.4 的代码。我有一个主脚本“master001.py”,我从中执行代码。它看起来像这样:
import sys
before = [str(m) for m in sys.modules]
from importlib import reload
import time
#from child001 import calculation as calc
import child001
from child002 import calculation_two
from child003 import calculation_three
after = [str(m) for m in sys.modules]
print("########################")
print([m for m in after if not m in before])
print("########################\n")
stop = False
while stop == False:
print("\n\n\n\n\n\n\n")
reload_child_one = input("reload child 1 function? Enter Y or N\n")
reload_child_one = reload_child_one.lower()
if reload_child_one == "y":
print("Script will try to reload the calculation 1 / child 1 module.")
time.sleep(1)
reload(child001)
reload_child_two = input("reload child 2 function? Enter Y or N\n")
reload_child_two = reload_child_two.lower()
if reload_child_two == "y":
print("Script will try to reload the calculation 2 / child 2 module.")
time.sleep(1)
#reload(sys.modules[calculation_two.__module__])
#del calculation_two
#from child002 import calculation_two
#__import__("child002", fromlist='calculation_two')
calculation_two = reload(sys.modules["child002"]).calculation_two
print("\n####################################################")
a = input("Enter number that will be saved in variable 'a' or enter Q to quit prorgam\n")
if a.lower() == "q" :
stop = True
print("\nFunction complted. Script will quit.")
print("####################################################\n")
time.sleep(2)
else:
try:
a = int(a)
print("Master - Launching Child function 'calculation'")
b = child001.calculation(a)
print("\nMaster - Inside Master file. Result = b = {}".format(b))
print("####################################################\n")
print("Master - Launching Child 2 function 'calculation_two' on input variable")
c = calculation_two(a)
print("\nMaster - Inside Master file. Result = c = {}".format(c))
print("####################################################\n")
print("Master - Launching child 3")
calculation_three()
time.sleep(2)
except:
print("input value was not a valid number. Please, try again.\n")
print("####################################################\n")
time.sleep(2)
master001.py 调用 child001.py 进行简单的计算:
print("wassupp from child 1 !!!")
def calculation(a):
print("\n----------------------------------------")
print("Child 1 - function 'calculation' started.")
print("Child 1 - Operation that will be executed is: input variable + 20")
result = a + 20
print("Child 1 - Returning result = {}".format(result))
print("----------------------------------------\n")
return result
然后,master001.py 调用 child002.py 在其中执行另一个简单的计算:
print("wassupp from child 2 !!!")
def calculation_two(a):
print("\n----------------------------------------")
print("Child 2 - function 'calculation_two' started.")
print("Child 2 - Operation that will be executed is: input variable + 200")
result = a + 200
print("Child 2 - Returning result = {}".format(result))
print("----------------------------------------\n")
return result
到目前为止,一切都很好。最后,我有 child003.py。在这个模块中,我执行一个实际从 child002.py 导入的计算
from child002 import calculation_two
print("wassupp from child 3 !!!")
def calculation_three():
print("\n----------------------------------------")
print("Child 3 function - Calculation will use the one in child 2 applied to value '3'.!\n")
result = calculation_two(3)
print("Child 3 - result = {}".format(result))
print("----------------------------------------\n")
return
正如您从运行 master001.py 中看到的那样,当我使用重新加载calculation_two 时
calculation_two = reload(sys.modules["child002"]).calculation_two
这适用于calculation_two
run from child002.py
,但它不会重新加载calculation_two
by 调用child003.py
。
更具体地说,如果您master001.py
在手动输入任何内容之前运行并更改 的内容calculation_two
,那么当您被问到时
reload child 1 function? Enter Y or N
你输入N,当你被问到
reload child 2 function? Enter Y or N
你输入Y,你会看到child003.py
不反映新更新代码的返回值。
我阅读了如何卸载(重新加载)Python 模块?以及如何重新加载使用 `from module import *` 导入的 python 模块,它们非常有帮助,但我找不到解决这个特定问题的方法。