47

There is one thing, that I do not understand.

Why does this

import scipy # happens with several other modules, too. I took scipy as an example now...

matrix = scipy.sparse.coo_matrix(some_params)

produce this error:

AttributeError: 'module' object has no attribute 'sparse'
Aufwind
  • 25,310
  • 38
  • 109
  • 154
  • 3
    What's so bothersome about that? What do you expect to happen if `sparse` does not exist in the module? – Paul Manta Jan 01 '12 at 23:36
  • 1
    I didn't realize, that sparse is a *submodule* of scipy, and that submodules are ***not** imported automatically* - as @David Zaslavsky pointed out below. – Aufwind Jan 01 '12 at 23:43
  • 17
    downvoters should explain why they downvoted, I don't see what's wrong with this question. In fact, I think it's a very good question – juliomalegria Jan 02 '12 at 02:43
  • 1
    Thanks @julio.alegria, its good to hear that from an other person. :-) – Aufwind Jan 02 '12 at 12:37

4 Answers4

63

This happens because the scipy module doesn't have any attribute named sparse. That attribute only gets defined when you import scipy.sparse.

Submodules don't automatically get imported when you just import scipy; you need to import them explicitly. The same holds for most packages, although a package can choose to import its own submodules if it wants to. (For example, if scipy/__init__.py included a statement import scipy.sparse, then the sparse submodule would be imported whenever you import scipy.)

David Z
  • 128,184
  • 27
  • 255
  • 279
  • 1
    I didn't know, that submodules are not automatically get imported. Thanks! – Aufwind Jan 01 '12 at 23:41
  • 1
    It actually depends on the package whether submodules are automatically imported as well. – Ethan Furman Jan 02 '12 at 07:53
  • @Ethan: true, I edited a note to that effect into the answer. – David Z Jan 02 '12 at 07:58
  • 6
    I think it's worth noting that the main reason for this is the existence of case insensitive file systems (i.e. Windows). On such a system, it can be difficult to know whether a file "sparse.py" should be imported as a module called `sparse`, or `Sparse`, or even `SParse` or `sParse` (if it had something to do with parsing S-expressions, perhaps). Python elects to guarantee consistent behaviour on case sensitive and case insensitive file systems by requiring an explicit import to tell it the proper name (either in your program or in the package `__init__.py`). – Ben Jan 02 '12 at 08:15
  • 1
    @Ben, I feel like yours is the best answer to this question. – tumultous_rooster Apr 14 '14 at 20:31
5

Because you imported scipy, not sparse. Try from scipy import sparse?

Dan
  • 10,531
  • 2
  • 36
  • 55
4

AttributeError is raised when attribute of the object is not available.

An attribute reference is a primary followed by a period and a name:

attributeref ::= primary "." identifier

To return a list of valid attributes for that object, use dir(), e.g.:

dir(scipy)

So probably you need to do simply: import scipy.sparse

kenorb
  • 155,785
  • 88
  • 678
  • 743
-3

The default namespace in Python is "__main__". When you use import scipy, Python creates a separate namespace as your module name. The rule in Pyhton is: when you want to call an attribute from another namespaces you have to use the fully qualified attribute name.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47