导入numpy和Scipy包的模块/子包的差异 [英] Differences in importing modules/subpackages of numpy and Scipy packages

查看:220
本文介绍了导入numpy和Scipy包的模块/子包的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Anaconda 2.1.0发行版使用scipy和numpy。我使用Spyder作为我的Python IDE。

I am using scipy and numpy through Anaconda 2.1.0 distribution. I use Spyder as my Python IDE.

当我运行将scipy导入sp 时,我无法访问子包,例如optimize,linalg,cluster等,通过 sp。

When I run import scipy as sp, I can't access the subpackages, such as optimize, linalg, cluster etc. through sp.

然而,当我运行 import numpy as np ,我可以通过 np

However, when I run import numpy as np, I am able to access all its subpackages, such as linalg, random, matrixlib, polynomial, testing, etc. through np.

这两种进口是否有不同的原因?为什么导入scipy为sp 没有将所有scipy子包都抓到 sp 的命名空间?

Is there a reason why the two imports work in different ways? Why does import scipy as sp not grab all scipy subpackages into sp's namespace?

推荐答案

通过设计python语言会出现这种不同导入行为的可能性。

This possibility of different import behaviour occurs by design of the python language.

导入语句默认情况下,模块(*)只导入主模块,而不导入子模块。主模块可能(如 numpy ),或者可能不会(如 scipy )导入部分或全部子模块。

An import statement of a module(*) by default only imports the main module, and not the submodules. The main module may (like in the case of numpy) , or may not (like scipy) import some or all the submodules.

背后的原因以 scipy 为例:在大多数情况下,你只需要一个子模块 scipy 包的内容。这个默认行为不会挂起解释器加载代码不必要的子模块。

The reason behind this is exemplified by scipy: in most cases, you will need only one submodule of the scipy package. This default behaviour will not hang the interpreter at loading submodules that are unnecessary to your code.

编辑:
注意 numpy 默认不导入所有子模块,例如它不加载 numpy.f2py ,参见问题/答案了解更多详情。

Notice that numpy does not import by default all the submodules, for example it does not load numpy.f2py, see THIS question/answer for more details.

(*)这里我指的是一个导入语句,如 import scipy import scipy as sp ,其中模块已加载。当然如果你写 import scipy.optimize 那么python将首先加载主模块,然后加载子模块。

(*) here I mean an import statement like import scipy or import scipy as sp, where a module is loaded. Of course if you write import scipy.optimize then python will first load the main module, and then the submodule.

这篇关于导入numpy和Scipy包的模块/子包的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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