python reload 无法从 sys.modules 中找到模块

发布于 2025-01-03 00:13:40 字数 1347 浏览 1 评论 0原文

我正在尝试重新加载已在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑轻吟梦一曲 2025-01-10 00:13:41

加载模块后,要重新加载它,您必须执行以下操作

module = reload(module)

Once you have loaded a module, to reload it you must do

module = reload(module)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文