34

This may well be completely trivial.

I want to call the spearmanr function from scipy: (the data are just examples)

import scipy
from numpy import *

Len = [2,3,5,7,2]
Pop = [5,2,6,3,2]

a = array(Len)
b = array(Pop)

print scipy.stats.spearmanr(a,b)

This generates the error:

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

What am I doing wrong?

Thanks

WillJones
  • 907
  • 1
  • 9
  • 19

1 Answers1

53

Use import scipy.stats. Then it works. Importing a package does not automatically import all the subpackages/modules. In these cases you have to do this explicitly.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • 4
    Why is that needed when you can access members of the `os.path` module when importing `os` on its own? Does `os` automatically import `os.path`? – JAB Jul 12 '11 at 13:41
  • 3
    Yes, it appears that it does. This is up to the package. – Björn Pollex Jul 12 '11 at 13:42
  • @Space_C0wb0y hmm when I try this its giving me a different error... "no matching architecture in universal wrapper" Could you post your entire correct modification of my code? – WillJones Jul 12 '11 at 14:15
  • @Will: The code as you posted, with my single modification of the `import`-statement works fine for me. This seems to be an issue with your installation of SciPy. – Björn Pollex Jul 12 '11 at 14:39
  • 3
    @Jab: That is needed because scipy employs lazy import to reduce the startup time. – Arne Babenhauserheide Sep 18 '13 at 09:13