防止 Python 缓存导入的模块 [英] Prevent Python from caching the imported modules

查看:19
本文介绍了防止 Python 缓存导入的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中使用 IPython 开发大型项目(拆分为多个文件和文件夹)时,我遇到了缓存导入模块的麻烦.

While developing a largeish project (split in several files and folders) in Python with IPython, I run into the trouble of cached imported modules.

问题是指令import module 只读取该模块一次,即使该模块已更改!因此,每次我更改包中的某些内容时,我都必须退出并重新启动 IPython.痛苦.

The problem is that instructions import module only reads the module once, even if that module has changed! So each time I change something in my package, I have to quit and restart IPython. Painful.

有什么方法可以正确强制重新加载某些模块?或者,更好的是,以某种方式阻止 Python 缓存它们?

Is there any way to properly force reloading some modules? Or, better, to somehow prevent Python from caching them?

我尝试了几种方法,但都没有奏效.特别是我遇到了非常非常奇怪的错误,比如一些模块或变量神秘地变得等于 None...

I tried several approaches, but none works. In particular I run into really, really weird bugs, like some modules or variables mysteriously becoming equal to None...

我发现的唯一合理的资源是 重新加载 Python 模块,来自 pyunit,但我没有检查过.我想要这样的东西.

The only sensible resource I found is Reloading Python modules, from pyunit, but I have not checked it. I would like something like that.

一个不错的选择是让 IPython 重新启动,或者以某种方式重新启动 Python 解释器.

A good alternative would be for IPython to restart, or restart the Python interpreter somehow.

那么,如果你用 Python 开发,你找到了解决这个问题的方法吗?

So, if you develop in Python, what solution have you found to this problem?

编辑

为了清楚起见:显然,我知道一些依赖于模块先前状态的旧变量可能会保留下来.那个我能接受.为什么在 Python 中强制重新加载模块而不发生各种奇怪的错误如此困难?

To make things clear: obviously, I understand that some old variables depending on the previous state of the module may stick around. That's fine by me. By why is that so difficult in Python to force reload a module without having all sort of strange errors happening?

更具体地说,如果我将整个模块放在 one 文件 module.py 中,那么以下工作正常:

More specifically, if I have my whole module in one file module.py then the following works fine:

import sys
try:
    del sys.modules['module']
except AttributeError:
    pass
import module

obj = module.my_class()

这段代码运行良好,我可以在几个月不退出 IPython 的情况下进行开发.

This piece of code works beautifully and I can develop without quitting IPython for months.

然而,每当我的模块由几个子模块组成时,就会崩溃:

However, whenever my module is made of several submodules, hell breaks loose:

import os
for mod in ['module.submod1', 'module.submod2']:
    try:
        del sys.module[mod]
    except AttributeError:
        pass
# sometimes this works, sometimes not. WHY?

为什么我的模块在一个大文件或几个子模块中对 Python 来说如此不同?为什么这种方法行不通??

Why is that so different for Python whether I have my module in one big file or in several submodules? Why would that approach not work??

推荐答案

退出并重新启动解释器是最好的解决方案.任何类型的实时重新加载或无缓存策略都不会无缝地工作,因为来自不再存在的模块的对象可以存在,并且因为模块有时会存储状态,并且因为即使您的用例确实允许热重新加载它也太复杂了,无法考虑值得.

Quitting and restarting the interpreter is the best solution. Any sort of live reloading or no-caching strategy will not work seamlessly because objects from no-longer-existing modules can exist and because modules sometimes store state and because even if your use case really does allow hot reloading it's too complicated to think about to be worth it.

这篇关于防止 Python 缓存导入的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆