python模块导入的问题

从模块导入函数时,通常可以使用

import   somemodule

或者

from  somemodule  import  somefunction

或者

from  somemodule import   somefunction  , anotherfunction  ,yetanotherfunction

或者

from  somemodule  import  *

只有确定自己想要从给定模块导入所有功能时,才应该使用最后一个版本,但是两个模块都有同一个函数的话怎么办呢?

只需要使用第一种方式导入,然后向下面这样使用函数:

module1.open(   )

module2.open(    )

但还有另外的选择:可以在语句末尾加一个as子句,在该子句后给出想要的别名,例如为整个模块提供别名:

import   math   as   foobar

foobar.sqrt(4)

又或者为函数提供别名:

from   math   import   sqrt   as   foobar

foobar(4)
原文地址:https://www.cnblogs.com/sunshinewang/p/6789626.html