我担心这是一个混乱的方式来处理问题,但..。
假设我想在 Python 中根据某些条件进行一些导入。
基于这个原因,我想写一个函数:
def conditional_import_modules(test):
if test == 'foo':
import onemodule, anothermodule
elif test == 'bar':
import thirdmodule, and_another_module
else:
import all_the_other_modules
现在我怎样才能使导入的模块全局可用呢?
例如:
conditional_import_modules(test='bar')
thirdmodule.myfunction()