0

我有以下问题,我将分享四个不同的 .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_tworun from child002.py,但它不会重新加载calculation_twoby 调用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 模块,它们非常有帮助,但我找不到解决这个特定问题的方法。

4

1 回答 1

1

您的问题在于您如何从以下位置导入函数child002

from child002 import calculation_two

这将创建对 中的函数对象的引用child003,并且该引用不会被替换。Python 名称就像字符串上的标签,与对象相关联。您可以将多个标签绑定到一个对象,如果您想用另一个对象替换该对象,那么您必须确保重新绑定所有这些标签。

你从这个开始:

sys.modules['child002']
    -> module object created from child002.py
        -> module.__dict__ (the module globals)
            -> module.__dict__['calculation_two']
                    |
                    |
                    +--> function object named calculation_two
                    |
                    |
            -> module.__dict__['calculation_two']
        -> module.__dict__ (the module globals)
    -> module object for child003.py
sys.modules['child003']

然后当你重新加载child002模块时,Python 用新对象替换所有现有的全局变量,所以现在你有:

sys.modules['child002']
    -> module object created from child002.py
        -> module.__dict__ (the module globals)
            -> module.__dict__['calculation_two']
                    |
                    |
                    +--> *new* function object named calculation_two


                    +--> *old* function object named calculation_two
                    |
                    |
            -> module.__dict__['calculation_two']
        -> module.__dict__ (the module globals)
    -> module object for child003.py
sys.modules['child003']

因为模块对象中的calculation_two引用child003是一个独立的标签。

您要么必须手动替换该标签:

calculation_two = reload(sys.modules["child002"]).calculation_two
child003.calculation_two = calculation_two

或者你不能calculation_two直接引用,而是只引用child002模块:

import child002

# ...

def calculation_three():
    # ...
    result = child002.calculation_two(3)

此时您具有以下关系:

sys.modules['child002']
    -> module object created from child002.py
       ^ -> module.__dict__ (the module globals)
       |    -> module.__dict__['calculation_two']
       |            |
       |            |
       |            +--> function object named calculation_two
       |
       |
       +------------+
                    |
                    |
            -> module.__dict__['child002']
        -> module.__dict__ (the module globals)
    -> module object for child003.py
sys.modules['child003']

我可以推荐阅读Ned Batchelder 对 Python 名称和值的解释,以换一种观点。

于 2018-10-09T20:52:46.647 回答