Why can't I import from a module alias?

后端 未结 4 654
栀梦
栀梦 2020-11-30 12:09

I just stumbled across this unexpected behavior in python (both 2.7 and 3.x):

>>> import re as regexp
>>> regexp


        
4条回答
  •  情歌与酒
    2020-11-30 13:07

    When from import is used python tries to look in the from file to import what you have requested. This might make it clearer.

    import re as regexp
    
    from regexp import search 
    

    This essentially asks python to look in a file called 'regexp' which it can't find. This is why the alias won't work.

提交回复
热议问题