Python class attributeError, even though I have that attribute - Stack Overflow
Is there someway I can get specific details about an AttributeError exception in Python? - Stack Overflow
web crawler - Getting Started with Python: Attribute Error - Stack Overflow
python - AttributeError and lost exception message - Stack Overflow
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.
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!
And what does that error generally mean?
An Attribute in Python is a name belonging to an object - a method or a variable. An AttributeError means that the program tried to use an attribute of an object, but the object did not have the requested attribute.
For instance, string objects have the 'upper' attribute, which is a method that returns the uppercase version of the string. You could write a method that uses it like this:
def get_upper(my_string):
return my_string.upper()
However, note that there's nothing in that method to ensure that you have to give it a string. You could pass in a file object, or a number. Neither of those have the 'upper' attribute, and Python will raise an Attribute Error.
As for why you're seeing it in this instance, you haven't provided enough detail for us to work it out. Add the full error message to your question.
1) Put code in Try ... Except block. get exception details.
2) Could you tell StackTrace details means which line # and method thrown error
And also are you able to run other simple python scripts without any error. Means just try to run some sample script etc.