出于好奇,如何取消导入
一个模块?
例如,您像这样导入几个模块:
import x, y, z
然后说出于某种原因您不想和/或不再需要包含模块 y
,您将如何仅 un-导入
模块y
而不影响模块x
或z
?
提前谢谢你。
最佳答案
来自文档:
Import statements are executed in two steps: (1) find a module, and initialize it if necessary; (2) define a name or names in the local namespace (of the scope where the import statement occurs).
如果您临时需要一个模块并且担心弄乱全局命名空间,您可以在本地范围内导入并且全局命名空间不会受到影响。
据我所知,import
语句的第一步无法撤消。
关于python - 如何处理 "un-importing"一个 SINGLE 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37701838/