32

I have an issue with importing the scipy.special package. It isn't harmful, just annoying/interesting.

When I import scipy using import scipy as sp and then try to access sp.special I get:

>>> import scipy as sp
>>> sp.special
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'special'
>>>

but if I then do import scipy.special I can access the special module through scipy.special and sp.special:

>>> import scipy as sp
>>> import scipy.special
>>> scipy.special
<module 'scipy.special' from 'C:\Python27\lib\site-packages\scipy\special\__init__.pyc'>
>>> sp.special
<module 'scipy.special' from 'C:\Python27\lib\site-packages\scipy\special\__init__.pyc'>
>>>

So I now have the special module accessible through both sp and scipy namespaces. The interesting bit is that I can access the rest of scipy through the scipy namespace.

First question: Why does the special module not import first time round?

Second question: How can I get access to the special module through the sp namespace only, without defining the scipy namespace?

Edit: using Python 2.7.2 and scipy 0.10.1

TuringTux
  • 559
  • 1
  • 12
  • 26
Iain Rist
  • 976
  • 2
  • 9
  • 18

1 Answers1

48

By default, "import scipy" does not import any subpackage. There are too many subpackages with large Fortran extension modules that are slow to load. I do not recommend doing import scipy or the abbreviated import scipy as sp. It's just not very useful. Use from scipy import special, from scipy import linalg, etc.

Robert Kern
  • 13,118
  • 3
  • 35
  • 32
  • Thanks, I will bear that in mind in future. Is there anywhere in the docs that explicitly states this? I can't see such a comment in the [API](http://docs.scipy.org/doc/scipy-0.10.1/reference/api.html) document – Iain Rist Mar 22 '12 at 14:21
  • 2
    I know it's an old comment, but it is still a FAQ, so here's one resource: http://docs.scipy.org/doc/scipy/reference/api.html#guidelines-for-importing-functions-from-scipy – Warren Weckesser Oct 27 '15 at 23:47