python reload 无法从 sys.modules 中找到模块
我正在尝试重新加载已在 sys.modules 中加载的模块,但不知何故, reload() 似乎不起作用。 (虽然compile()+load可以工作,但我仍然不能使用它,因为它是通过twisted重新加载代码的)
for loader, module_name, is_pkg in pkgutil.walk_packages(main_module.__path__):
if(not module_name in sys.modules):
module = loader.find_module(module_name).load_module(module_name)
else:
module = sys.modules.get(module_name)
#this is unhelpful
#sys.path.append(module.__path__)
#this works
#imp.find_module(module.__name__, module.__path__)
#this doesn't
reload(module)
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/celery/execute/trace.py", line 36, in trace
return cls(states.SUCCESS, retval=fun(*args, **kwargs))
File "/usr/lib64/python2.7/site-packages/celery/app/task/__init__.py", line 232, in __call__
return self.run(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/celery/app/__init__.py", line 172, in run
return fun(*args, **kwargs)
File "/home/myuser/Dropbox/job-pipe/job_pipe/apps/tasks/crawl.py", line 71, in crawl
crawler = CrawlerScript()
File "/home/myuser/Dropbox/job-pipe/job_pipe/apps/tasks/crawl.py", line 37, in __init__
reload(module)
ImportError: No module named example
重新加载的正确方法是什么?我认为这是由find_module引起的,尽管它似乎工作正常,并且错误没有帮助。
谢谢。
I'm trying to reload module that's already loaded within sys.modules, but somehow, reload() doesn't seem to work.
(compile()+load would work although, still I can't use that since it's code reload through twisted)
for loader, module_name, is_pkg in pkgutil.walk_packages(main_module.__path__):
if(not module_name in sys.modules):
module = loader.find_module(module_name).load_module(module_name)
else:
module = sys.modules.get(module_name)
#this is unhelpful
#sys.path.append(module.__path__)
#this works
#imp.find_module(module.__name__, module.__path__)
#this doesn't
reload(module)
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/celery/execute/trace.py", line 36, in trace
return cls(states.SUCCESS, retval=fun(*args, **kwargs))
File "/usr/lib64/python2.7/site-packages/celery/app/task/__init__.py", line 232, in __call__
return self.run(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/celery/app/__init__.py", line 172, in run
return fun(*args, **kwargs)
File "/home/myuser/Dropbox/job-pipe/job_pipe/apps/tasks/crawl.py", line 71, in crawl
crawler = CrawlerScript()
File "/home/myuser/Dropbox/job-pipe/job_pipe/apps/tasks/crawl.py", line 37, in __init__
reload(module)
ImportError: No module named example
What's the proper way to reload? I thought it was caused by find_module, though it seems to work properly, and error is not helpful.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
加载模块后,要重新加载它,您必须执行以下操作
Once you have loaded a module, to reload it you must do