重新加载本地模块不起作用

Reloading local module isn't working

首先,我知道之前已经发布过,但是要么 A) 建议不起作用,要么 B) 建议手动从命名空间中删除模块并像往常一样重新导入它。

我有以下模块结构

basedir/
    pytools/
        __init__.py
        tools.py
    setup.py
    test.py

如果我在 basedir 中,则导入 pytools 并创建 class testcls 的对象。 class 的实际属性可在 tools.py 中找到。 testcls 有一个名为 testfunc 的方法,它现在简单地打印出 AAA

>>> import pytools
>>> test = pytools.testcls()
>>> test.testfunc()
AAA

假设我将 testfunc() 更改为现在打印出 BBB。我这样做,并保存文件。然后我重新加载模块并重试,它没有打印出 BBB:

>>> from importlib import reload
>>> reload(pytools)
>>> test = pytools.testcls()
>>> test.testfunc()
AAA

但是如果我执行完全相同的过程但改为更改 test.py,将该文件作为模块导入,编辑其中的函数,然后重新加载它,它的行为符合预期:

>>> import test
>>> testvariable = test.testcls()
>>> testvariable.testfunc2()
AAA
# Change the function here
>>> from importlib import reload
>>> reload(test)
>>> testvariable = test.testcls()
>>> testvariable.testfunc2()
BBB

我真的不明白发生了什么,这让我很烦。这也花费了我很多时间,但此刻我更恼火。

有什么想法吗??

版本:

Python: 3.6.5

口译员:IPython, 6.2.1

让我们更笼统地命名:

basedir/
    testpackage/
        __init__.py
        testmodule.py
    test.py

如果testmodule.py包含:

class TestClass:
    def test_method(self):
        print("AAA")

那么下面的工作就如你所料:

>>> from testpackage import testmodule
>>> obj = testmodule.TestClass()
>>> obj.test_method()
DDD
>>> # === Edit ===
>>> from importlib import reload
>>> reload(testmodule)
>>> obj = testmodule.TestClass()
>>> obj.test_method()
EEE

但是,如果 __init__.py 有类似的东西:

from .testmodule import TestClass

并且您尝试导入(并重新加载)而不是模块,发生以下情况:

>>> import testpackage
>>> obj = testpackage.TestClass()
>>> obj.test_method()
EEE
>>> # === Edit ===
>>> from importlib import reload
>>> reload(testpackage)
>>> obj = testpackage.TestClass()
>>> obj.test_method()
EEE

(不变)

请注意以下部分 of the docs

If a module imports objects from another module using from … import …, calling reload() for the other module does not redefine the objects imported from it — one way around this is to re-execute the from statement, another is to use import and qualified names (module.name) instead.

如果您要重新加载 模块和包,按此顺序,它将再次按预期工作:

>>> import testpackage
>>> obj = testpackage.TestClass()
>>> obj.test_method()
HHH
>>> # === Edit ===
>>> from importlib import reload
>>> reload(testpackage.testmodule)
>>> reload(testpackage)
>>> obj = testpackage.TestClass()
>>> obj.test_method()
III

但这看起来很傻而且容易出错,就用第一个例子中的方法吧:

from testpackage import testmodule
...
reload(testmodule)
...