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

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

问题描述

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

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

问题在于说明导入模块只读取模块一次,即使该模块已更改!因此,每次我在包中更改某些内容时,都必须退出并重新启动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?

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

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...

我找到的唯一明智的资源是来自pyunit的重新加载Python模块,但我有没检查过。我想要这样的东西。

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?

更具体地说,如果我将整个模块放在中一个文件 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?

为什么我的模块在一个大文件或多个子模块中有这么不同?为什么这种方法不起作用?

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天全站免登陆