为什么有些包需要用 'from' 导入,而其他包需要用 'import' 导入?
Why do some packages need to be imported with 'from' and others with 'import'?
当我这样做时...
import numpy as np
...我可以使用它但是...
import pprint as pp
...不能,因为我需要这样做...
from pprint import pprint as pp
并且还有 __import__(str(module))
并且可能更多隐藏在文档中。
我读了一些书,'import module' or 'from module import' for instance, but the answers are more targeted towards making a choice on which to use. Also, python-how-to-import-other-python-files 只是提供了更多关于利弊的见解。
有人能解释一下为什么会有差异吗?使用不同类型的导入时幕后发生了什么,它们是如何工作的?
区别在于pprint模块包含一个叫做pprint的函数。所以当你 运行
import pprint as pp
您需要调用 pp.pprint
来引用该函数。
然而,Numpy 暴露在顶层,所有 functions/classes 嵌套在其中。
当你说
When I do this ...
import numpy as np
... it is callable
你错了。 numpy
不可调用。它是一个模块。
>>> import numpy as np
>>> np()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
模块提供的函数可调用:
>>> np.arange(5)
array([0, 1, 2, 3, 4])
同样,pprint
模块不可调用。它是一个模块。 pprint.pprint
函数 可调用:
>>> import pprint as pp
>>> pp.pprint([1, 2, 3])
[1, 2, 3]
不需要将 from
与 pprint
一起使用,也无需将 from
与 numpy
一起使用。 from
导入只是从模块中提取特定内容;例如,from pprint import pprint as pp
为您提供 pprint.pprint
函数作为 pp
。您基本上不需要以一种或另一种方式执行导入。
导入模块时,python 需要在文件系统中找到它并将其分配给模块中的某个变量名。各种形式允许您分配不同的本地名称 ("as something") 或进入模块并将其内部对象之一分配给本地名称 ("from ...")。
import numpy # imports numpy and names it "numpy"
import numpy as np # imports numpy and names it "np"
from pprint import pprint # imports pprint anonymously, finds an
# object in pprint called "pprint"
# and names it "pprint"
from pprint import pprint as pp # imports pprint anonymously, finds an
# object in pprint called "pprint"
# and names it "pp"
当我这样做时...
import numpy as np
...我可以使用它但是...
import pprint as pp
...不能,因为我需要这样做...
from pprint import pprint as pp
并且还有 __import__(str(module))
并且可能更多隐藏在文档中。
我读了一些书,'import module' or 'from module import' for instance, but the answers are more targeted towards making a choice on which to use. Also, python-how-to-import-other-python-files 只是提供了更多关于利弊的见解。
有人能解释一下为什么会有差异吗?使用不同类型的导入时幕后发生了什么,它们是如何工作的?
区别在于pprint模块包含一个叫做pprint的函数。所以当你 运行
import pprint as pp
您需要调用 pp.pprint
来引用该函数。
然而,Numpy 暴露在顶层,所有 functions/classes 嵌套在其中。
当你说
When I do this ...
import numpy as np
... it is callable
你错了。 numpy
不可调用。它是一个模块。
>>> import numpy as np
>>> np()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
模块提供的函数可调用:
>>> np.arange(5)
array([0, 1, 2, 3, 4])
同样,pprint
模块不可调用。它是一个模块。 pprint.pprint
函数 可调用:
>>> import pprint as pp
>>> pp.pprint([1, 2, 3])
[1, 2, 3]
不需要将 from
与 pprint
一起使用,也无需将 from
与 numpy
一起使用。 from
导入只是从模块中提取特定内容;例如,from pprint import pprint as pp
为您提供 pprint.pprint
函数作为 pp
。您基本上不需要以一种或另一种方式执行导入。
导入模块时,python 需要在文件系统中找到它并将其分配给模块中的某个变量名。各种形式允许您分配不同的本地名称 ("as something") 或进入模块并将其内部对象之一分配给本地名称 ("from ...")。
import numpy # imports numpy and names it "numpy"
import numpy as np # imports numpy and names it "np"
from pprint import pprint # imports pprint anonymously, finds an
# object in pprint called "pprint"
# and names it "pprint"
from pprint import pprint as pp # imports pprint anonymously, finds an
# object in pprint called "pprint"
# and names it "pp"