我有一个长期运行的Python服务器,希望能够在不重新启动服务器的情况下升级服务。做这件事最好的方法是什么?
if foo.py has changed:
unimport foo <-- How do I do this?
import foo
myfoo = foo.Foo()
发布于 2017-11-10 02:36:53
我在尝试重新加载Sublime Text中的内容时遇到了很多问题,但最终我可以根据sublime_plugin.py
用来重新加载模块的代码,编写这个实用程序来重新加载Sublime Text上的模块。
下面接受您从路径中重新加载名称上带有空格的模块,然后在重新加载后,您可以像往常一样导入模块。
def reload_module(full_module_name):
"""
Assuming the folder `full_module_name` is a folder inside some
folder on the python sys.path, for example, sys.path as `C:/`, and
you are inside the folder `C:/Path With Spaces` on the file
`C:/Path With Spaces/main.py` and want to re-import some files on
the folder `C:/Path With Spaces/tests`
@param full_module_name the relative full path to the module file
you want to reload from a folder on the
python `sys.path`
"""
import imp
import sys
import importlib
if full_module_name in sys.modules:
module_object = sys.modules[full_module_name]
module_object = imp.reload( module_object )
else:
importlib.import_module( full_module_name )
def run_tests():
print( "\n\n" )
reload_module( "Path With Spaces.tests.semantic_linefeed_unit_tests" )
reload_module( "Path With Spaces.tests.semantic_linefeed_manual_tests" )
from .tests import semantic_linefeed_unit_tests
from .tests import semantic_linefeed_manual_tests
semantic_linefeed_unit_tests.run_unit_tests()
semantic_linefeed_manual_tests.run_manual_tests()
if __name__ == "__main__":
run_tests()
如果您是第一次运行,这应该会加载模块,但如果稍后您可以再次使用方法/函数run_tests()
,它将重新加载测试文件。对于Sublime Text (Python 3.3.6
),这种情况经常发生,因为它的解释器永远不会关闭(除非您重新启动Sublime Text,即Python3.3
解释器)。
https://stackoverflow.com/questions/437589
复制相似问题