Import * from module by importing via string

后端 未结 2 431
攒了一身酷
攒了一身酷 2021-01-14 07:14

I know I can use importlib to import modules via a string. How can I recreate the import * functionality using this library? Basically, I want some

2条回答
  •  温柔的废话
    2021-01-14 08:03

    Here is shorten version for @HaiVu answer which refers to this solution from @Bakuriu

    import importlib
    #import the module
    mod = importlib.import_module('collections')
    #make the variable global
    globals().update(mod.__dict__)
    

    Note:

    • This will import lots of things beside the user-defined variables

    • @HaiVu solution did the best of it ie. only import user-defined variables

提交回复
热议问题