reload (update) a module file in the interpreter

后端 未结 3 1088
抹茶落季
抹茶落季 2020-12-15 23:35

Let\'s say I have this python script script.py and I load it in the interpreter by typing

import script

and then I execute my

3条回答
  •  佛祖请我去吃肉
    2020-12-15 23:57

    An alternative solution that has helped me greatly is to maintain a copy of sys.modules keys and pop the new modules after the import to force re-imports of deep imports:

    >>> oldmods = set(sys.modules.keys())
    >>> import script
    >>> # Do stuff
    >>> for mod in set(sys.modules.keys()).difference(oldmods): sys.modules.pop(mod)
    >>> import script
    

提交回复
热议问题