如何取消导入已经导入的python模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32234156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:15:05  来源:igfitidea点击:

How to unimport a python module which is already imported?

pythonpython-import

提问by chanwcom

I'm quite new with NumPy/SciPy. But these days, I've started using it very actively for numerical calculation instead of using Matlab.

我对 NumPy/SciPy 很陌生。但是最近,我开始非常积极地使用它进行数值计算,而不是使用 Matlab。

For some simple calculations, I do just in the interactive mode rather than writing a script. In this case, are there any ways to unimport some modules which was already imported? Unimporting might not needed when I write python programs, but in the interactive mode, it is needed.

对于一些简单的计算,我只是在交互模式下进行,而不是编写脚本。在这种情况下,有什么方法可以取消导入一些已经导入的模块?编写python程序时可能不需要取消导入,但在交互模式下,则需要取消导入。

回答by chanwcom

While you shouldn't worry about "unimporting" a module in Python, you can normally just simply decrement the reference to the imported module or function using del:

虽然您不必担心在 Python 中“取消导入”模块,但您通常只需使用以下命令减少对imported 模块或函数的引用del

>>> import requests
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'readline', 'requests', 'rlcompleter']
>>> del requests
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'readline', 'rlcompleter']
>>>

Note that I'd advise just not worrying about this as the overhead of an unused import is near trivial -- traversing one extra entry in sys.modulesis nothing compared to the false security del some_modulewill give you (consider if the __init__does some setup or you ran from X import *).

请注意,我建议不要担心这一点,因为未使用的导入的开销几乎是微不足道的 -sys.modules与错误的安全性相比,遍历一个额外的条目并不算什么del some_module(考虑是否进行了__init__一些设置或您运行了from X import *)。

回答by Mark Ransom

There's no way to unload something once you've imported it. Python keeps a copy of the module in a cache, so the next time you import it it won't have to reload and reinitialize it again.

导入后无法卸载。Python 将模块的副本保存在缓存中,因此下次导入它时不必重新加载和重新初始化它。

If all you need is to lose access to it, you can use del:

如果您只需要失去对它的访问权限,您可以使用del

import package
del package

If you've made a change to a package and you want to see the updates, you can reloadit. Note that this won't work in some cases, for example if the imported package also needs to reload a package it depends on. You should read the relevant documentation before relying on this.

如果您对软件包进行了更改,并且想要查看更新,则可以reload这样做。请注意,这在某些情况下不起作用,例如如果导入的包还需要重新加载它所依赖的包。在依赖于此之前,您应该阅读相关文档。

For Python versions up to 2.7, reloadis a built-in function:

对于高达 2.7 的 Python 版本,reload是一个内置函数:

reload(package)

For Python versions 3.0 to 3.3 you can use imp.reload:

对于 Python 3.0 到 3.3 版本,您可以使用imp.reload

import imp
imp.reload(package)

For Python versions 3.4 and up you can use importlib.reload:

对于 Python 3.4 及更高版本,您可以使用importlib.reload

import importlib
importlib.reload(package)

回答by DeepSOIC

When you import a module, Python looks if this module is in sys.modules dictionary. If not, it runs the module, and puts the resulting module object in sys.modules.

当你导入一个模块时,Python 会检查这个模块是否在 sys.modules 字典中。如果没有,它运行模块,并将生成的模块对象放入sys.modules.

So, to unimport a module, one might use

因此,要取消导入模块,可以使用

import sys
sys.modules.pop('your_module_name_here')

The next time it is imported, python will re-execute the module. However, in all other loaded modules that have imported the module, the old code will still be used, as they still hold the reference to the old module object. reloadfunction explained in other answers helps with that situation by updating the module object rather than creating a brand new one. But it doesn't help with from your_module import some_functionstyle of imports.

下次导入时,python 会重新执行该模块。但是,在所有其他已导入模块的加载模块中,仍将使用旧代码,因为它们仍持有对旧模块对象的引用。reload其他答案中解释的函数通过更新模块对象而不是创建一个全新的对象来帮助解决这种情况。但这from your_module import some_function对导入样式没有帮助。