1

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Matematikisto
  • 707
  • 6
  • 8

1 Answers1

4

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 [*]
 ...
xnx
  • 24,509
  • 11
  • 70
  • 109
  • 3
    See also [this](http://stackoverflow.com/questions/27744767/differences-in-importing-modules-subpackages-of-numpy-and-scipy-packages) question about why is that – gg349 Jan 13 '15 at 14:46
  • Thanks both of you, for the solution and for the aditional info in that other question. – Matematikisto Jan 13 '15 at 15:02