Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this AttributeError in python occur?

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' 
like image 539
Aufwind Avatar asked Jan 01 '12 23:01

Aufwind


People also ask

How do I fix AttributeError in Python?

Solution for AttributeError Errors and exceptions in Python can be handled using exception handling i.e. by using try and except in Python. Example: Consider the above class example, we want to do something else rather than printing the traceback Whenever an AttributeError is raised.

What does AttributeError mean in Python?

What is a Python AttributeError? A Python AttributeError is raised when you try to call an attribute of an object whose type does not support that method. For instance, trying to use the Python append() method on a string returns an AttributeError because strings do not support append() .

Why am I getting AttributeError object has no attribute?

If you are getting an object that has no attribute error then the reason behind it is because your indentation is goofed, and you've mixed tabs and spaces.

How do you fix an object with no attribute in Python?

The Python "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on a list. To solve the error, access the list element at a specific index or correct the assignment.


1 Answers

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.)

like image 117
David Z Avatar answered Sep 23 '22 18:09

David Z