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

前端 未结 5 2046
暖寄归人
暖寄归人 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:06

    It's probably a very bad idea to let the user choose what to import. Packages can execute code on import, so you're effectively allowing a user to arbitrarily execute code on your system! Much safer to do something like

    if user_input == 'file1.py':
      from files import file1 as file
    elif user_input == 'file2.py':
      from files import file2 as file
    else:
      file = None
      print "Sorry, you can't import that file"
    

提交回复
热议问题