How to do multiple imports in Python?

前端 未结 5 2189
我寻月下人不归
我寻月下人不归 2020-12-05 04:48

In Ruby, instead of repeating the \"require\" (the \"import\" in Python) word lots of times, I do

%w{lib1 lib2 lib3 lib4 lib5}.each { |x| require x }
         


        
5条回答
  •  有刺的猬
    2020-12-05 05:07

    For known module, just separate them by commas:

    import lib1, lib2, lib3, lib4, lib5
    

    If you really need to programmatically import based on dynamic variables, a literal translation of your ruby would be:

    modnames = "lib1 lib2 lib3 lib4 lib5".split()
    for lib in modnames:
        globals()[lib] = __import__(lib)
    

    Though there's no need for this in your example.

提交回复
热议问题