How do I unload (reload) a Python module?

后端 未结 20 2405
轮回少年
轮回少年 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:33

    The following code allows you Python 2/3 compatibility:

    try:
        reload
    except NameError:
        # Python 3
        from imp import reload
    

    The you can use it as reload() in both versions which makes things simpler.

提交回复
热议问题