繁体   English   中英

如何强制ipython深度重载?

[英]how to force ipython deep reload?

我通过运行魔法在ipython中运行脚本:

%run myscript.py

这个脚本导入了我编写的各种模块,并且我经常在运行之间进行更改。 我想自动重新加载这些模块,而不必重新启动ipython。 有关stackoverflow和其他推荐的问题有很多问题

%load_ext autoreload
%autoreload 2

也许还有

 %aimport <your module>

投入了很好的措施。 但这根本行不通。 是否超出了autoreload能够做的深度重载? 是否有其他方法可以删除所有已加载的模块,或者以静默方式重启ipython所依赖的python后台进程?

编辑:已经玩了这个更多,似乎自动重载失败更微妙。 可能是(我还不是100%确定)自动重载仅在模块的__init__.py正在执行时失败from .<some file in the module> import *模块中的from .<some file in the module> import * ,而不是按名称导入每个成员。 我也尝试了%reset magic,但这只是为了清空命名空间,它不会清除缓存的模块。

顺便说一下,Spyder能够强制重新加载模块,但我不确定这是怎么做的(ipython的某种包装器会重启进程?)

我抬头Spyder的解决方案,它主要采用delsys.modules 下面我稍微修改了我在~/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py找到的代码,我把它放在一个名为reloader.py的模块中:

import sys


def _is_module_deletable(modname, modpath):
    if modname.startswith('_cython_inline'):
        # Don't return cached inline compiled .PYX files
        return False
    for path in [sys.prefix]:
        if modpath.startswith(path):
            return False
    else:
        return set(modname.split('.'))


def clear():
    """
    Del user modules to force Python to deeply reload them

    Do not del modules which are considered as system modules, i.e.
    modules installed in subdirectories of Python interpreter's binary
    Do not del C modules
    """
    log = []
    for modname, module in list(sys.modules.items()):
        modpath = getattr(module, '__file__', None)

        if modpath is None:
            # *module* is a C module that is statically linked into the
            # interpreter. There is no way to know its path, so we
            # choose to ignore it.
            continue

        if modname == 'reloader':
            # skip this module
            continue

        modules_to_delete = _is_module_deletable(modname, modpath)
        if modules_to_delete:
            log.append(modname)
            del sys.modules[modname]

    print("Reloaded modules:\n\n%s" % ", ".join(log))

现在只需执行import reloader然后调用reloader.clear()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM