当我使用 sp.integrate.quad() 时,将 scipy 作为 sp 导入时出错

Error importing scipy as sp when I use sp.integrate.quad()

我在将 scipy 导入 Python 时遇到错误。当我写:

import scipy as sp
x2 = lambda x: x**2
print sp.integrate.quad(x2, 0, 4)

我收到错误:

sp.integrate.quad: "NameError: name 'integrate' is not defined".

为什么会出现此错误?

导入scipy 不会自动加载integrate 子包。使用:

from scipy.integrate import quad

import scipy.integrate as spi

并使用spi.quad

来自文档(或者更确切地说,SciPy 的 __init__.py 文件):

...
Subpackages
-----------
Using any of these subpackages requires an explicit import.  For example,
``import scipy.cluster``.

::

 cluster                      --- Vector Quantization / Kmeans
 fftpack                      --- Discrete Fourier Transform algorithms
 ...
 integrate                    --- Integration routines [*]
 ...