How do I unload (reload) a Python module?

后端 未结 20 2343
轮回少年
轮回少年 2020-11-21 05:20

I have a long-running Python server and would like to be able to upgrade a service without restarting the server. What\'s the best way do do this?

if foo.py          


        
20条回答
  •  逝去的感伤
    2020-11-21 05:48

    Other option. See that Python default importlib.reload will just reimport the library passed as an argument. It won't reload the libraries that your lib import. If you changed a lot of files and have a somewhat complex package to import, you must do a deep reload.

    If you have IPython or Jupyter installed, you can use a function to deep reload all libs:

    from IPython.lib.deepreload import reload as dreload
    dreload(foo)
    

    If you don't have Jupyter, install it with this command in your shell:

    pip3 install jupyter
    

提交回复
热议问题