我在尝试让 python 接受“全局”导入时遇到了问题
在一个模块中,它需要根据另一个变量导入另一个模块,但如果我在启动函数中有它,它似乎并没有将它导入到所有模块函数中;例如:
def start():
selected = "web"
exec("from gui import " + selected + " as ui")
log("going to start gui " + selected)
ui.start()
这有效,但在同一个模块中:
def close():
ui.stop()
没用。我不知道这里发生了什么
乔
最佳答案
import gui
ui = None
def start():
selected = "web"
log("going to start gui " + selected)
global ui
__import__("gui.%s" % selected) # if you're importing a submodule that
# may not have been imported yet
ui = getattr(gui, selected)
ui.start()
关于python导入模块在全局范围内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1991784/