How do I import variable packages in Python like using variable variables ($$) in PHP?

前端 未结 5 2056
暖寄归人
暖寄归人 2020-11-29 00:05

I want to import some package depending on which value the user chooses.

The default is file1.py:



        
5条回答
  •  天涯浪人
    2020-11-29 01:07

    Old thread, but I needed the answer, so someone else still might...

    There's a cleaner way to do this in Python 2.7+:

    import importlib
    
    
    my_module = importlib.import_module("package.path.%s" % module_name)
    

提交回复
热议问题