How do I unload (reload) a Python module?

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

    for me for case of Abaqus it is the way it works. Imagine your file is Class_VerticesEdges.py

    sys.path.append('D:\...\My Pythons')
    if 'Class_VerticesEdges' in sys.modules:  
        del sys.modules['Class_VerticesEdges']
        print 'old module Class_VerticesEdges deleted'
    from Class_VerticesEdges import *
    reload(sys.modules['Class_VerticesEdges'])
    

提交回复
热议问题