如果我只是从 numpy 模块引用它,为什么我必须从 numpy 导入它 [英] Why do I have to import this from numpy if I am just referencing it from the numpy module

查看:128
本文介绍了如果我只是从 numpy 模块引用它,为什么我必须从 numpy 导入它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阿罗哈!

我有两块代码,一块可以工作,另一块不行.唯一的区别是我不使用的 numpy 模块的注释代码行.当我从不引用npm"时,为什么我需要导入该模型?

I have two blocks of code, one that will work and one that will not. The only difference is a commented line of code for a numpy module I don't use. Why am I required to import that model when I never reference "npm"?

此命令有效:

import numpy as np
import numpy.matlib as npm

V  = np.array([[1,2,3],[4,5,6],[7,8,9]])
P1 = np.matlib.identity(V.shape[1], dtype=int)
P1

此命令不起作用:

import numpy as np
#import numpy.matlib as npm

V  = np.array([[1,2,3],[4,5,6],[7,8,9]])
P1 = np.matlib.identity(V.shape[1], dtype=int)
P1

上面得到这个错误:

AttributeError: 'module' object has no attribute 'matlib'

提前致谢!

推荐答案

Short Answer

这是因为numpy.matlibnumpy的可选子包,必须单独导入.

Short Answer

This is because numpy.matlib is an optional sub-package of numpy that must be imported separately.

此功能的原因可能是:

  • 特别是对于 numpynumpy.matlib 子模块重新定义了 numpy 的函数以返回矩阵而不是 ndarrays,一个可选的许多人可能不想要的功能
  • 更一般地,加载父模块而不加载许多用户可能不经常需要的潜在加载缓慢的模块
  • 可能是命名空间分离
  • In particular for numpy, the numpy.matlib sub-module redefines numpy's functions to return matrices instead of ndarrays, an optional feature that many may not want
  • More generally, to load the parent module without loading a potentially slow-to-load module which many users may not often need
  • Possibly, namespace separation

当你只导入 numpy 而没有子包 matlib 时,Python 将寻找 .matlib 作为 .matlib 的一个属性code>numpy 包.如果没有导入 numpy.matlib(见下面的讨论)

When you import just numpy without the sub-package matlib, then Python will be looking for .matlib as an attribute of the numpy package. This attribute has not been assigned to numpy without importing numpy.matlib (see discussion below)

如果您想知道为什么 np.matlib.identity 无需使用关键字 npm 就可以工作,那是因为当您导入子模块 matlib,父模块 numpy(在你的例子中名为 np)将被赋予一个属性 matlib,它绑定到子模块模块.这仅在您首先定义 numpy 时才有效.

If you're wondering why np.matlib.identity works without having to use the keyword npm, that's because when you import the sub-module matlib, the parent module numpy (named np in your case) will be given an attribute matlib which is bound to the sub-module. This only works if you first define numpy.

来自参考:

当使用任何机制(例如 importlib API、import 或 import-from 语句或内置 import())加载子模块时,将在父模块的命名空间中放置一个绑定到子模块对象.

When a submodule is loaded using any mechanism (e.g. importlib APIs, the import or import-from statements, or built-in import()) a binding is placed in the parent module’s namespace to the submodule object.

导入和 __init__.py

导入内容的选择由模块目录中模块各自的 __init__.py 文件决定.您可以使用 dir() 函数查看各个模块定义的名称.

Importing and __init__.py

The choice of what to import is determined in the modules' respective __init__.py files in the module directory. You can use the dir() function to see what names the respective modules define.

>> import numpy

>> 'matlib' in dir(numpy)
# False

>> import numpy.matlib

>> 'matlib' in dir(numpy)
# True

或者,如果直接查看__init__.pynumpy 文件,您会看到 matlib 没有导入.

Alternatively, if you look directly at the __init__.py file for numpy you'll see there's no import for matlib.

如果您想知道如何平滑地复制命名空间;

If you're wondering how the namespace is copied over smoothly;

matlib 源代码 运行此命令以复制 numpy 命名空间:

import numpy as np                                    # (1)
...
# need * as we're copying the numpy namespace
from numpy import *                                   # (2)
...
__all__ = np.__all__[:] # copy numpy namespace        # (3)

第(2)行,from numpy import *尤为重要.因此,您会注意到,如果您只导入 numpy.matlib,您仍然可以使用所有 numpy 模块,而无需导入 numpy

Line (2), from numpy import * is particularly important. Because of this, you'll notice that if you just import numpy.matlib you can still use all of numpy modules without having to import numpy!

如果没有第 (2) 行,第 (3) 行中的命名空间副本将仅附加到子模块.有趣的是,由于第 (3) 行,您仍然可以执行这样一个有趣的命令.

Without line (2), the namespace copy in line (3) would only be attached to the sub-module. Interestingly, you can still do a funny command like this because of line (3).

import numpy.matlib               
numpy.matlib.np.matlib.np.array([1,1])

这是因为 np.__all__ 附加到 numpy.matlibnp(通过第 (1) 行导入).

This is because the np.__all__ is attached to the np of numpy.matlib (which was imported via line (1)).

这篇关于如果我只是从 numpy 模块引用它,为什么我必须从 numpy 导入它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆