how to reload a Class in python shell?

后端 未结 6 1855
既然无缘
既然无缘 2020-12-07 10:47

If I import a module defining a class of the same name belonging to a package, it is imported as a Class, not a Module because of the __init__.py of the parent package. See

6条回答
  •  Happy的楠姐
    2020-12-07 11:06

    There are thee ways to solve this:

    1. Use import MyPak.MyMod instead of from MyPak import MyMod

    Then you can write:

    from importlib import reload  # If on Python 3
    import MyPak.MyMod
    reload(MyPak.MyMod)
    

    and it works.

    2. Use IPython.lib.deepreload

    from MyPak import MyMod
    from IPython.lib.deepreload import reload
    reload(MyPak)  # This should also reload all submodules
    

    3. Use autoreload magic

    %load_ext autoreload
    %autoreload 2
    import MyPak.MyMod  # All changes to MyPak.MyMod will be loaded automatically
    

提交回复
热议问题