import vs __import__( ) vs importlib.import_module( )?

后端 未结 2 1235
傲寒
傲寒 2020-12-07 20:50

I noticed Flask was using Werkzeug to __import__ a module, and I was a little confused. I went and checked out the docs on it and saw that it seems to give you

2条回答
  •  难免孤独
    2020-12-07 21:12

    __import__(import_name), so I don't see how that's any different from just using the import statement

    Both __import__() and importlib.import_module() allow you to import a module when you have the module name as a string. You cannot write:

    x = 're'
    import x
    

    or you'll get:

     File "1.py", line 3, in 
     import x ImportError: No module named x
    

提交回复
热议问题