[英]Difference between import numpy and import numpy as np
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? 有人可以解释一下吗?
numpy is the top package name, and doing import numpy
doesn't import submodule numpy.f2py
. numpy是顶级软件包名称,而
import numpy
不会导入子模块numpy.f2py
。
When you do import numpy
it creats a link that points to numpy
, but numpy
is not further linked to f2py
. 当你
import numpy
是外币指向一个链接numpy
,但numpy
不进一步连接到f2py
。 The link is established when you do import 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.f2py
and import numpy.f2py as myf2py
: 以下是
import numpy.f2py
和import numpy.f2py as myf2py
之间的差异import numpy.f2py as myf2py
:
import numpy.f2py
import numpy.f2py as myf2py
The import as
syntax was introduced in PEP 221 and 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
. numpy包绑定到局部变量
numpy
。 The import as
syntax 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). 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 numpy
actually 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. 导入子模块时 ,必须引用完整的父模块名称,因为导入机制发生在比局部变量范围更高的级别。 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]". 我也断言你的断言“在可能的情况下应该[导入numpy as np]”。 This is done for historical reasons, mostly because people get tired very quickly of prefixing every operation with
numpy
. 这是出于历史原因而完成的,主要是因为人们很快就为每个操作添加了
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
) 它从来没有阻止过我的名字冲突(程序员的懒惰实际上暗示了与
np
发生冲突的可能性更高)
Finally, to round out my exposé, here are 2 interesting uses of the import as
mechanism that you should be aware of: 最后,为了完善我的展示,以下是
import as
两个有趣用途import as
您应该注意的机制:
import scipy.ndimage.interpolation as warp
warp.affine_transform(I, ...)
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
This is a language feature. 这是一种语言功能。
f2py
is a subpackage of the module numpy
and must be loaded separately. f2py
是模块numpy
的子包,必须单独加载。
This feature allows: 此功能允许:
numpy
only the packages you need, speeding up execution. numpy
加载你需要的软件包,加快执行速度。 f2py
to have namespace separation from the developers of another subpackage. f2py
的开发人员将命名空间与另一个子包的开发人员分开。 Notice however that import numpy.f2py
or its variant import numpy.f2py as myf2py
are 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 AttributeError
because f2py
is not an attribute of numpy
, because the __init__()
of the package numpy
did not declare in its scope anything about the subpackage f2py
. 您收到一个
AttributeError
因为f2py
不是numpy
的属性,因为包numpy
的__init__()
没有在其范围内声明有关子包f2py
任何内容。
numpy.f2py
is actually a submodule of numpy
, and therefore has to be imported separately from numpy. numpy.f2py
实际上是numpy
的子模块,因此必须与numpy分开导入。 As aha said before: 正如阿哈所说:
When you do import numpy it creats a link that points to numpy, but numpy is not further linked to f2py.
当你导入numpy时,它会创建一个指向numpy的链接,但是numpy没有进一步链接到f2py。 The link is established when you do import 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. 当您将语句
import numpy as np
,您将短语“numpy”缩短为“np”以使代码更易于阅读。 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.)
(tkinter和ttk是当你遇到这个问题时会发生什么的一个很好的例子.UI看起来非常不同。)
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. 但它没有任何导入f2py子包。 That's the reason that a statement like
这就是声明的原因
import numpy as np
np.f2py
won't work but 不会工作但是
import numpy as np
np.core
will work. 将工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.