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'
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 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() .
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.
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.
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
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With