How to reload python module imported using `from module import *`

前端 未结 8 2010
挽巷
挽巷 2020-12-02 14:12

I saw in this useful Q&A that one can use reload(whatever_module) or, in Python 3, imp.reload(whatever_module).

My question is, what if

8条回答
  •  甜味超标
    2020-12-02 14:48

    When importing using from whatever_module import whatever, whatever is counted as part of the importing module, so to reload it - you should reload your module. But just reloading your module you will still get the old whatever - from the already-imported whatever_module, so you need to reload(whatever_module), and than reload your module:

    # reload(whatever_module), if you imported it
    reload(sys.modules['whatever_module'])
    reload(sys.modules[__name__])
    

    if you used from whatever_module import whatever you can also consider

    whatever=reload(sys.modules['whatever_module']).whatever
    

    or

    whatever=reload(whatever_module).whatever
    

提交回复
热议问题