如何从函数中进行全局导入?
- 论坛
- 如何从函数中进行全局导入?
5 浏览
如何从函数中进行全局导入?
虽然我担心这种方法会让问题变得混乱,但是...假设我想根据一些条件在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()