Matematikisto
Matematikisto

Reputation: 719

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

I'm getting an error when importing scipy into Python. When I write:

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

I get the error:

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

Why am I getting this error?

Upvotes: 1

Views: 3821

Answers (1)

xnx
xnx

Reputation: 25550

Importing scipy does not automatically load the integrate subpackage. Use:

from scipy.integrate import quad

or

import scipy.integrate as spi

and use spi.quad

From the docs (or, rather, SciPy's __init__.py file):

...
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 [*]
 ...

Upvotes: 4

Related Questions