Python import numpy 和 import numpy as np 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22622571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between import numpy and import numpy as np
提问by user1318806
I understand that when possible one should use
我知道在可能的情况下应该使用
import numpy as np
This helps keep away any conflict due to namespaces. But I have noticed that while the command below works
这有助于避免由于命名空间引起的任何冲突。但我注意到虽然下面的命令有效
import numpy.f2py as myf2py
the following does not
以下不
import numpy as np
np.f2py #throws no module named f2py
Can someone please explain this?
有人可以解释一下吗?
回答by aha
numpyis the top package name, and doing import numpydoesn't import submodule numpy.f2py.
numpy是顶级包名称,并且 doimport numpy不会导入 submodule numpy.f2py。
When you do import numpyit creats a link that points to numpy, but numpyis not further linked to f2py. The link is established when you do import numpy.f2py
当您这样做时,import numpy它会创建一个指向 的链接numpy,但numpy不会进一步链接到f2py。当你这样做时,链接就建立起来了import numpy.f2py
In your above code:
在你上面的代码中:
import numpy as np # np is an alias pointing to numpy, but at this point numpy is not linked to numpy.f2py
import numpy.f2py as myf2py # this command makes numpy link to numpy.f2py. myf2py is another alias pointing to numpy.f2py as well
Here is the difference between import numpy.f2pyand import numpy.f2py as myf2py:
这里的区别import numpy.f2py和import numpy.f2py as myf2py:
import numpy.f2py- put numpyinto local symbol table(pointing to numpy), and numpy is linked to numpy.f2py
- both numpyand numpy.f2pyare accessible
import numpy.f2py as myf2py- put my2pyinto local symbol table(pointing to numpy.f2py)
- Its parent numpy is not added into local symbol table. Therefore you can not access numpy directly
import numpy.f2py- 将numpy放入本地符号表(指向numpy),numpy链接到numpy.f2py
- 既numpy的和numpy.f2py都可以访问
import numpy.f2py as myf2py- 将my2py放入本地符号表(指向 numpy.f2py)
- 它的父 numpy 没有添加到本地符号表中。因此你不能直接访问 numpy
回答by Ben Schwabe
numpy.f2pyis actually a submodule of numpy, and therefore has to be imported separately from numpy. As ahasaid before:
numpy.f2py实际上是 的子模块numpy,因此必须与 numpy 分开导入。正如阿哈之前所说:
When you do import numpy it creats a link that points to numpy, but numpy is not further linked to f2py. The link is established when you do import numpy.f2py
当您导入 numpy 时,它会创建一个指向 numpy 的链接,但 numpy 不会进一步链接到 f2py。当您导入 numpy.f2py 时建立链接
when you call the statement import numpy as np, you are shortening the phrase "numpy" to "np" to make your code easier to read. It also helps to avoid namespace issues. (tkinter and ttk are a good example of what can happen when you do have that issue. The UIs look extremely different.)
当您调用该语句时import numpy as np,您将短语“numpy”缩短为“np”以使您的代码更易于阅读。它还有助于避免命名空间问题。(tkinter 和 ttk 是一个很好的例子,说明当你遇到这个问题时会发生什么。UI 看起来非常不同。)
回答by gg349
This is a language feature. f2pyis a subpackage of the module numpyand must be loaded separately.
这是语言功能。f2py是模块的子包,numpy必须单独加载。
This feature allows:
此功能允许:
- you to load from
numpyonly the packages you need, speeding up execution. - the developers of
f2pyto have namespace separation from the developers of another subpackage.
- 你
numpy只从你需要的包加载,加速执行。 - 的开发人员
f2py将命名空间与另一个子包的开发人员分开。
Notice however that import numpy.f2pyor its variant import numpy.f2py as myf2pyare still loading the parent module numpy.
但是请注意,import numpy.f2py它或其变体import numpy.f2py as myf2py仍在加载父模块numpy。
Said that, when you run
说,当你跑
import numpy as np
np.f2py
You receive an AttributeErrorbecause f2pyis not an attribute of numpy, because the __init__()of the package numpydid not declare in its scope anything about the subpackage f2py.
您收到一个AttributeError因为f2py不是 的属性numpy,因为__init__()包的numpy没有在其范围内声明关于子包的任何内容f2py。
回答by hbristow
The import assyntax was introduced in PEP 221and is well documented there.
该import as语法是在PEP 221中引入的,并且在那里有很好的记录。
When you import a module via
当您通过以下方式导入模块时
import numpy
the numpy package is bound to the local variable numpy. The import assyntax simply allows you to bind the import to the local variable name of your choice (usually to avoid name collisions, shorten verbose module names, or standardize access to modules with compatible APIs).
numpy 包绑定到局部变量numpy。该import as语法仅允许您将导入绑定到您选择的局部变量名称(通常是为了避免名称冲突、缩短冗长的模块名称或标准化对具有兼容 API 的模块的访问)。
Thus,
因此,
import numpy as np
is equivalent to,
相当于,
import numpy
np = numpy
del numpy
When trying to understand this mechanism, it's worth remembering that import numpyactually means import numpy as numpy.
在尝试理解这种机制时,值得记住它的import numpy实际含义是import numpy as numpy.
When importing a submodule, you must refer to the full parent module name, since the importing mechanics happen at a higher level than the local variable scope. i.e.
导入子模块时,您必须引用完整的父模块名称,因为导入机制发生在比局部变量范围更高的级别。IE
import numpy as np
import numpy.f2py # OK
import np.f2py # ImportError
I also take issue with your assertion that "where possible one should [import numpy as np]". This is done for historical reasons, mostly because people get tired very quickly of prefixing every operation with numpy. It has never prevented a name collision for me (laziness of programmers actually suggests there's a higher probability of causing a collision with np)
我也对您的“在可能的情况下应该 [import numpy as np]”的断言表示异议。这样做是出于历史原因,主要是因为人们很快就厌倦了为每个操作添加前缀numpy。它从来没有阻止过我的名字冲突(程序员的懒惰实际上表明导致与 冲突的可能性更高np)
Finally, to round out my exposé, here are 2 interesting uses of the import asmechanism that you should be aware of:
最后,为了完善我的披露,这里有两个import as你应该注意的机制的有趣用法:
1. long subimports
1. 长子进口
import scipy.ndimage.interpolation as warp
warp.affine_transform(I, ...)
2. compatible APIs
2. 兼容的 API
try:
import pyfftw.interfaces.numpy_fft as fft
except:
import numpy.fft as fft
# call fft.ifft(If) with fftw or the numpy fallback under a common name
回答by Prashant Ujjwal
Well quite an old post but here are my 2 cents over the explanation provided by others.
好吧,这是一篇很老的帖子,但这是我对其他人提供的解释的 2 美分。
numpy (refer git repository) package have various subpackages, f2py is one of them other are as core, ma etc
numpy(参考 git 存储库)包有各种子包,f2py 是其中之一,其他是作为核心,ma 等
If you refer the init.py in numpy package it has imports like -
如果你在 numpy 包中引用init.py 它有像 -
from . import core etc
but it's not having any import for f2py subpackage. That's the reason that a statement like
但它对 f2py 子包没有任何导入。这就是像这样的语句的原因
import numpy as np
np.f2py
won't work but
不会工作,但
import numpy as np
np.core
will work.
将工作。

