how to reload a Class in python shell?

后端 未结 6 1839
既然无缘
既然无缘 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条回答
  • 2020-12-07 10:51

    It works for me using python 3.5.2:

    import importlib
    importlib.reload(class)
    from class import module
    
    0 讨论(0)
  • 2020-12-07 10:54

    I finally found the answer:

    import MyPak
    from MyPak import MyMod
    

    after editing MyPak/MyMod.py file, to reload the class MyMod in the file MyMod.py, one needs to

    import sys
    del sys.modules['MyPak.MyMod'] 
    reload(MyPak)
    from MyPak import MyMod
    

    Caveats:

    1. Executing del MyPak or del MyMod or del MyPak.MyMod does not solve the problem since it simply removes the name binding. Python only searches sys.modules to see whether the modules had already been imported. Check out the discussion in the post module name in sys.modules and globals().

    2. When reloading MyPak, python tries to execute the line from MyMod import MyMod in MyPak/__init__.py. However, it finds MyPak.MyMod in sys.modules, thus it will NOT reload MyMod although MyPak/MyMod.py has been updated. And you will find that no new MyPak/MyMod.pyc is generated.

    0 讨论(0)
  • 2020-12-07 10:56

    I have one myfile.py file which contains one class MyClass

    To import just do:

    from myfile import MyClass
    mc = MyClass()
    

    To reload:

    import sys
    del sys.modules['myfile']
    from myfile import MyClass
    modifiedmc = MyClass()
    

    This is very useful while building modules. one can put these inside a function and just call the function

    def myreload():
       import sys
       del sys.modules['myfile']
       from myfile import MyClass
       modifiedmc = MyClass()
       global mc
       mc = modifiedmc
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-07 11:13

    You can use a magic function:

    %load_ext autoreload
    %autoreload 2
    from MyPak import MyMod
    

    It also works for function imports:

    %load_ext autoreload
    %autoreload 2
    from anypythonfile import my_function
    

    This magic function works in Python 2.x (I've tested on 2.7+) and Python 3.x (I've tested on 3.7).

    0 讨论(0)
  • 2020-12-07 11:14

    On Python 3 only, import the reload function:

    >>> from importlib import reload
    

    On both Python 2.x, and 3.x, you can then simply call reload on the module:

    >>> import MyPak
    >>> reload(MyPak)
    >>> from MyPak import MyMod
    

    However, instances of the old class will not be updated (there's simply no code that describes the update mechanism).

    0 讨论(0)
提交回复
热议问题