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

Answer from David Z on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 74394435 › python-class-attributeerror-even-though-i-have-that-attribute
Python class attributeError, even though I have that attribute - Stack Overflow
I'm making some code with pygame and for some twisted, wicked reason I get an attributeError when obviosly I have that atrribute. What is even more interesting that I only get error at the second if
Top answer
1 of 3
16

AttributeError typically identifies the missing attribute. e.g.:

class Foo:
    def __init__(self):
        self.a = 1

f = Foo()
print(f.a)
print(f.b)

When I run that, I see:

$ python foo.py
1
Traceback (most recent call last):
  File "foo.py", line 10, in <module>
    print(f.b)
AttributeError: Foo instance has no attribute 'b'

That's pretty explicit. If you're not seeing something like that, please post the exact error you're seeing.

EDIT

If you need to force the printing of an exception (for whatever reason), you can do this:

import traceback

try:
    # call function that gets AttributeError
except AttributeError:
    traceback.print_exc()

That should give you the full error message and traceback associated with the exception.

2 of 3
3

The traceback should alert you to the attribute access that raised the AttributeError exception:

>>> f.b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Foo instance has no attribute 'b'

Alternatively, convert the Exception to str:

>>> try:
...     f.b
... except AttributeError, e:
...     print e
... 
Foo instance has no attribute 'b'

If you want to get a list of the attributes available on an object, try dir() or help()

>>> dir(f)
['__doc__', '__init__', '__module__', 'a']

>>> help(str)
Help on class str in module __builtin__:

class str(basestring)
 |  str(object) -> string
 |  
 |  Return a nice string representation of the object.
 |  If the argument is a string, the return value is the same object.
 |  
 |  Method resolution order:
 |      str
 |      basestring
 |      object
 |  
 |  Methods defined here:
 |  
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |  
[...]
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T

You can even call help() on dir (why is left as an exercise for the reader):

>>> help(dir)
Help on built-in function dir in module __builtin__:

dir(...)

dir([object]) -> list of strings

If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
  for a module object: the module's attributes.
  for a class object:  its attributes, and recursively the attributes
    of its bases.
  for any other object: its attributes, its class's attributes, and
    recursively the attributes of its class's base classes.

Failing these... you could always look at the code, unless you've been provided some precompiled module by a third-party, in which case you should demand better documentation (say some unit tests!) from your supplier!

Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 30149538 › i-keep-getting-an-attributeerror-str-object-has-no-attribute-get-author › 30149696
python - I keep getting an AttributeError: 'str' object has no attribute '__get_author'. Could someone please help me with what I can do to fix it? - Stack Overflow
Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal. ... Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I am creating a tweet manager and i'm coming upon a attribute error that i honestly don't know how to fix. I am a novice at Python so I'm still learning. Why am I getting AttributeError: 'str' object has no attribute '__get_author'.
Top answer
1 of 5
8

Here:

File "/Users/name/anaconda/lib/python3.6/http/client.py", line 71, in <module>
    import email.parser
File "/Users/name/Desktop/Google Drive/FEBB/serverless/crwlr/email.py"
    from bs4 import BeautifulSoup

The local email.py in /Users/name/Desktop/Google Drive/FEBB/serverless/crwlr/ shadows the stdlib's one. Now in your local email.py module, you're importing bs4, which imports html5lib, which imports xml.sax.saxutils, which imports urllib.request, which wants to import http.

IOW you end up with an (accidental) circular dependencie. At this point the http module is only partially imported, and doesn't yet have defined "client", hence the error.

The simple solution is to rename your "email.py" module to something else, or (if it's only a script and not a module) move it out of your pythonpath.

EDIT: I just noticed that your code started by importing http, so the http module should be already fully loaded, so even if the problem with your email.py script/module needs to be fixed, this shouldn't lead to this problem. So chances are you have another http.py module or http package in your sys.path shadowing the stdlib's one. To debug this, add this line just after the import http one:

 print(http)

This should print something like:

<module 'http' from '/some/path/to/a/python/file.pyc`>

If the path is not the one to your python install stdlib's "http/init.pyc" then you found the offender. If it's one of your own scripts/modules, the fix is the same as for email.py.

2 of 5
1

Might be Bs4 is raising the exception, Kindly execute the below script in the existing one validate Bs4 import is working fine

try:
    from bs4 import BeautifulSoup
except Exception as err:
    raise ImportError('Bs4 is not imported correctly. - {}'.format(err))
🌐
Stack Overflow
stackoverflow.com › questions › 66047426 › attributeerror-in-python-program
AttributeError in Python program - Stack Overflow
Traceback (most recent call last): File "C:\Users\manis\PycharmProjects\WiFi_Hacker\Hack.py", line 9, in <module> print("{:<30}| {:<}".format(i.results[0])) AttributeError: 'str' object has no attribute 'results'
🌐
Carleton University
cs.carleton.edu › cs_comps › 1213 › pylearn › final_results › encyclopedia › attributeError.html
Error Encyclopedia | Attribute Error
This means that if we try to access the same attributes on a different object-type, we might cause Python to throw an error back at us. For example, if we try to use the append attribute of an integer, we will receive an AttributeError, because integer type objects do not possess that attribute:
🌐
Real Python
realpython.com › ref › builtin-exceptions › attributeerror
AttributeError | Python’s Built-in Exceptions – Real Python
Note that you need to manually raise AttributeError in .__getattr__() if you overwrite the default implementation with your custom logic. Otherwise, you break the default behavior of raising an AttributeError when looking up a non-existent attribute. ... In this tutorial, you'll get to know some of the most commonly used built-in exceptions in Python.
🌐
Python
docs.python.org › 3 › library › exceptions.html
Built-in Exceptions — Python 3.14.4 documentation
The base class for those built-in exceptions that are raised for various arithmetic errors: OverflowError, ZeroDivisionError, FloatingPointError.
🌐
GeeksforGeeks
geeksforgeeks.org › python-attributeerror
Python: AttributeError - GeeksforGeeks
January 3, 2023 - Suppose if the variable is list type then it supports the append method. Then there is no problem and not getting"Attribute error". Note: Attribute errors in Python are generally raised when an invalid attribute reference is made.
🌐
Stack Apps
stackapps.com › questions › 3951 › attribute-error-while-using-the-stackoverflow-api-wrapper-for-python-on-idle
feature request - Attribute error while using the stackoverflow api wrapper for python on IDLE - Stack Apps
February 27, 2013 - Traceback (most recent call last): File "C:\Users\Animesh\Desktop\NLP\metastack.py", line 3, in <module> from stackexchange import Site, StackOverflow File "C:\Users\Animesh\Desktop\stackexchange.py", line 4, in <module> AttributeError: 'module' object has no attribute 'StackOverflow'
🌐
Stack Overflow
stackoverflow.com › questions › tagged › attributeerror
Newest 'attributeerror' Questions - Stack Overflow
I have an xml file with experimental data that I am trying to read out in Python with pandas. The data is separated into 2 experiments, which each have 2 wavelengths (and more subnodes). I can select ... ... I have dicom files from different sources and sometimes i get error like this: AttributeError: 'FileDataset' object has no attribute 'PatientAge' because some files doesn't have this attribute.