reloading module which has been imported to another module

后端 未结 6 1041
难免孤独
难免孤独 2021-01-01 19:32

Let\'s face it, the whole business of reloading python code after changing it is a mess. I figured out awhile back that calling import at the in

相关标签:
6条回答
  • 2021-01-01 19:44

    Don't forget that an import is really just assigning a name in a namespace. So, you could reassign that name after reloading:

    >>> reload(module2)
    >>> module1.class1 = module2.class1
    

    Now the class1 object inside module1 refers to the reloaded version from module2.

    0 讨论(0)
  • 2021-01-01 19:51

    Rather than getting better at reloading modules, you could get better at restarting the interpreter. For example, you can put your setup code into its own file, and then run it like this:

    $ python -i setup.py
    >>>
    

    This will run setup.py, then leave you at the interactive prompt. Or, rather than doing a lot of work in the interactive prompt, write automated tests that do your work for you.

    You are right, reloading modules in Python is a mess. The semantics of the language make it difficult to change code while the process is running. Learn not to need reloading modules, you'll be happier.

    0 讨论(0)
  • 2021-01-01 19:51

    Here is a recursive reload function that you could use (credit to @Matthew): https://stackoverflow.com/a/17194836/1020625

    0 讨论(0)
  • 2021-01-01 20:00

    Have a look into IPython. It has the autoreload extension that automatically reloads modules during the interpreter session before calling functions within. I cite the example from the landing page:

    In [1]: %load_ext autoreload
    
    In [2]: %autoreload 2
    
    In [3]: from foo import some_function
    
    In [4]: some_function()
    Out[4]: 42
    
    In [5]: # open foo.py in an editor and change some_function to return 43
    
    In [6]: some_function()
    Out[6]: 43
    
    0 讨论(0)
  • 2021-01-01 20:02

    Ok, I'm not sure that qualifies as an answer without a change to the code, but... at least, that doesn't involve a change to module1.

    You can use some module wrapper class, that saves loaded modules before and after loading module1 and that provides a reload method, something like that:

    import sys
    
    class Reloader(object):
        def __init__(self, modulename):
            before = sys.modules.keys()
            __import__(modulename)
            after = sys.modules.keys()
            names = list(set(after) - set(before))
            self._toreload = [sys.modules[name] for name in names]
    
        def do_reload(self):
            for i in self._toreload:
                reload(i)
    

    Then load module1 with:

    reloader = Reloader('module1')
    

    Atfer that you can modify module2 and reload it in interpreter with:

    reloader.do_reload()
    
    0 讨论(0)
  • 2021-01-01 20:03

    To reload a module, you have to use reload, and you have to use it on the module you want to reload. Reloading a module doesn't recursively reload all modules imported by that module. It just reloads that one module.

    When a module is imported, a reference to it is stored, and later imports of that module re-use the already-imported, already-stored version. When you reload module1, it re-runs the from module2 import ... statement, but that just reuses the already-imported version of module2 without reloading it.

    The only way to fix this is to change your code so it does import module2 instead of (or in addition to) from module2 import .... You cannot reload a module unless the module itself has been imported and bound to a name (i.e., with an import module statement, not just a from module import stuff statement).

    Note that you can use both forms of the import, and reloading the imported module will affect subsequent from imports. That is, you can do this:

    >>> import module
    >>> from module import x
    >>> x
    2
    # Change module code here, changing x to 3
    >>> reload(module)
    >>> from module import x
    >>> x
    3
    

    This can be handy for interactive work, since it lets you use short, unprefixed names to refer to what you need, while still being able to reload the module.

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