代码之家  ›  专栏  ›  技术社区  ›  James Wright user3667089

重新加载本地模块不工作

  •  1
  • James Wright user3667089  · 技术社区  · 7 年前

    我有以下模块结构

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

    basedir ,并导入 pytools testcls tools.py . 有一个方法调用 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
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   jedwards    7 年前

    让我们更笼统地说出事情的名称:

    basedir/
        testpackage/
            __init__.py
            testmodule.py
        test.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 以下内容:

    >>> 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)
    ...