python - 如何处理 "un-importing"一个 SINGLE 模块?

标签 python python-3.x import module python-import

出于好奇,如何取消导入一个模块?

例如,您像这样导入几个模块:

import x, y, z

然后说出于某种原因您不想和/或不再需要包含模块 y,您将如何 un-导入 模块y 而不影响模块xz

提前谢谢你。

最佳答案

来自文档:

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/

相关文章:

python - 用 matplotlib 表示体素

python - 导入中断,因为库无法导入自己的模块

python - 使用 from-import 语句并且仍然需要在 Python 3 中输入类名

python - SQLObject:重用我的参数列表

python - 使用 Twisted/Autobahn Websockets 编写 "interactive"客户端

python - 制作具有多索引值的字典

Python 3 urllib 忽略 SSL 证书验证

python - 类型错误 : got multiple values for argument

c - python 列表调整大小的实现中的错误?

java - 我无法导入 org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer