Attributeerror python как исправить

We raise a Python AttributeError when we try to call or access an attribute of an object that does not exist for that object. This tutorial will go
blog banner for post titled: How to Solve Guide for Python AttributeError

We raise a Python AttributeError when we try to call or access an attribute of an object that does not exist for that object.

This tutorial will go through what an attribute is, what the AttributeError is in detail, and we will go through four examples to learn how to solve the error.

Table of contents

  • What is a Python AttributeError?
  • Example #1: Trying to Use append() on a String
    • Solution
  • Example #2: Trying to Access an Attribute of a Class that does not exist
    • Solution
  • Example #3: NoneType Object has no Attribute
    • Solution
  • Example #4: Handling Modules
    • Solution
  • Summary

What is a Python AttributeError?

An attribute of an object is a value or a function associated with that object. We can express calling a method of a class as referencing an attribute of a class.

Let’s look at an example of a Python class for the particle electron

class Electron:
    def __init__(self):
        self.charge = -1
        self.mass = 0.51
        self.spin = 1/2
 
    def positron(self):
        self.charge = +1
        return self.charge

We can think of an attribute in Python as a physical attribute of an object. In this example, the fundamental particle, the electron, has physical attributes of charge, mass, and spin. The Electron class has the attributes charge, mass, and spin.

An attribute can also be a function. The function positron() returns the charge of the electron’s anti-particle, the positron.

Data types can have attributes. For example, the built-in data type List has the append() method to append elements to an existing list. Therefore, List objects support the append() method. Let’s look at an example of appending to a list:

a_list = [2, 4, 6]

a_list.append(8)

print(a_list)

Attributes have to exist for a class object or a data type for you to reference it. If the attribute is not associated with a class object or data type, you will raise an AttributeError.

Example #1: Trying to Use append() on a String

Let’s look at an example scenario where we concatenate two strings by appending one string to another.

string1 = "research"

string2 = "scientist"

string1.append(string2)

Using append() is impossible because the string data type does not have the append() method. Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
1 string1.append(string2)

AttributeError: 'str' object has no attribute 'append'

Solution

To solve this problem, we need to define a third string. We can then concatenate the two strings using the + symbol and assign the result to the third string. We can concatenate a space between the two strings so that the words do not run together. Let’s look at how the revised code:

string1 = "research"

string2 = "scientist"

string3 = string1 + " " + string2

print(string3)
research scientist

Example #2: Trying to Access an Attribute of a Class that does not exist

Let’s look at an example scenario where we want to access an attribute of a class that does not exist. We can try to create an instance of the class Electron from earlier in the tutorial. Once we have the instance, we can try to use the function get_mass() to print the mass of the electron in MeV.

class Electron:

   def __init__(self):

       self.charge = -1

       self.mass = 0.51

       self.spin = 1/2
  
   def positron(self):

       self.charge = +1

       return self.charge

electron = Electron()

mass = electron.get_mass()

If we try to run the code, we get the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
1 mass = electron.get_mass()

AttributeError: 'Electron' object has no attribute 'get_mass'

The Electron class has no attribute called get_mass(). Therefore we raise an AttributeError.

Solution

To solve this, we can do two things. We can add the method to the class and use a try-except statement. First, let’s look at adding the method:

class Electron:
    def __init__(self):
        self.charge = -1
        self.mass = 0.51
        self.spin = 1/2

    def positron(self):
        self.charge = +1
        return self.charge

    def get_mass(self):
            return self.mass
electron = Electron()

mass = electron.get_mass()

print(f' The mass of the electron is {mass} MeV')

 The mass of the electron is 0.51 MeV

Secondly, let’s look at using try-except to catch the AttributeError. We can use try-except statements to catch any error, not just AttributeError. Suppose we want to use a method called get_charge() to get the charge of the electron object, but we are not sure whether the Electron class contains the get_charge() attribute. We can enclose the call to get_charge() in a try-except statement.

class Electron:

    def __init__(self):

        self.charge = -1

        self.mass = 0.51

        self.spin = 1/2

    def positron(self):

        self.charge = +1

        return self.charge

    def get_mass(self):

            return self.mass

electron = Electron()

try:

    charge = electron.get_charge()

except Exception as e:

    print(e)
'Electron' object has no attribute 'get_charge'

Using try-except statements aligns with professional development and makes your programs less prone to crashing.

Example #3: NoneType Object has no Attribute

NoneType means that whatever class or object you are trying to access is None. Therefore, whenever you try to do a function call or an assignment for that object, it will raise the AttributeError: ‘NoneType’ object has no attribute. Let’s look at an example scenario for a specific NoneType attribute error. We will write a program that uses regular expressions to search for an upper case “S” character at the beginning and print the word. We need to import the module re for regular expression matching.

import re

# Search for an upper case "S" character in the beginning of a word then print the word

string = "Research Scientist"

for i in string.split():

    x = re.match(r"bSw+", i)

    print(x.group())

Let’s run the code and see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
      1 for i in string.split():
      2     x = re.match(r"bSw+", i)
      3     print(x.group())
      4 

AttributeError: 'NoneType' object has no attribute 'group'

We raise the AttributeError because there is no match in the first iteration. Therefore x returns None. The attribute group() does not belong to NoneType objects.

Solution

To solve this error, we want to only call group() for the situation where there is a match to the regular expression. We can therefore use the try-except block to handle the AttributeError. We can use continue to skip when x returns None in the for loop. Let’s look at the revised code.

import re

# Search for an upper case "S" character in the beginning of a word then print the word

string = "Research Scientist"
for i in string.split():
    x = re.match(r"bSw+", i)
    try:
        print(x.group())
    except AttributeError:
        continue
Scientist

We can see that the code prints out Scientist, which is the word that has an upper case “S” character.

Example #4: Handling Modules

We can encounter an AttributeError while working with modules because we may call a function that does not exist for a module. Let’s look at an example of importing the math module and calling a function to perform a square root.

import math

number = 9

square_root_number = math.square_root(number)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
1 square_root_number = math.square_root(number)

AttributeError: module 'math' has no attribute 'square_root'

The module math does not contain the attribute square_root. Therefore we get an AttributeError.

Solution

To solve this error, you can use the help() function to get the module’s documentation, including the functions that belong to the module. We can use the help function on the math module to see which function corresponds to the square root.

import math

help(math)
  sqrt(x, /)

        Return the square root of x.

The function’s name to return the square root of a number is sqrt(). We can use this function in place of the incorrect function name.

square_root_number = math.sqrt(number)

print(square_root_number)
3.0

The code successfully returns the square root of 9. You can also use help() on classes defined in your program. Let’s look at the example of using help() on the Electron class.

help(Electron)

class Electron(builtins.object)
 |  Methods defined here:
 |  
 |  __init__(self)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  get_mass(self)
 |  
 |  positron(self)

The help() function returns the methods defined for the Electron class.

Summary

Congratulations on reading to the end of this tutorial. Attribute errors occur in Python when you try to reference an invalid attribute.

  • If the attribute you want for built-in data types does not exist, you should look for an attribute that does something similar. For example, there is no append() method for strings but you can use concatenation to combine strings.
  • For classes that are defined in your code, you can use help() to find out if an attribute exists before trying to reference it. If it does not exist you can add it to your class and then create a new instance of the class.
  • If you are not sure if a function or value does not exist or if a code block may return a NoneType object, you can wrap the code in a try-except statement. Using a try-except stops your program from crashing if you raise an AttributeError.

For further reading on AttributeError, you can go to the following article: How to Solve Python AttributeError: ‘list’ object has no attribute ‘split’

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

In every programming language, if we develop new programs, there is a high chance of getting errors or exceptions. These errors yield to the program not being executed. One of the error in Python mostly occurs is “AttributeError”. AttributeError can be defined as an error that is raised when an attribute reference or assignment fails. 
For example, if we take a variable x we are assigned a value of 10. In this process suppose we want to append another value to that variable. It’s not possible. Because the variable is an integer type it does not support the append method. So in this type of problem, we get an error called “AttributeError”. 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.
There are a few chances of getting AttributeError.
Example 1:

Python3

Output:

Traceback (most recent call last):
  File "/home/46576cfdd7cb1db75480a8653e2115cc.py", line 5, in 
    X.append(6)
AttributeError: 'int' object has no attribute 'append'

Example 2: Sometimes any variation in spelling will cause an Attribute error as Python is a case-sensitive language.

Python3

string = "The famous website is { }".fst("geeksforgeeks")

print(string)

Output:

Traceback (most recent call last):
  File "/home/2078367df38257e2ec3aead22841c153.py", line 3, in 
    string = "The famous website is { }".fst("geeksforgeeks")
AttributeError: 'str' object has no attribute 'fst'

Example 3: AttributeError can also be raised for a user-defined class when the user tries to make an invalid attribute reference.

Python3

class Geeks():

    def __init__(self):

        self.a = 'GeeksforGeeks'

obj = Geeks()

print(obj.a)

print(obj.b)

Output: 

GeeksforGeeks

Error:

Traceback (most recent call last):
  File "/home/373989a62f52a8b91cb2d3300f411083.py", line 17, in 
    print(obj.b)
AttributeError: 'Geeks' object has no attribute 'b'

Example 4:  AttributeError can also be raised for a user-defined class when the user misses out on adding tabs or spaces between their lines of code.

Python3

class dict_parsing:

    def __init__(self,a):

        self.a = a

        def getkeys(self):

            if self.notdict():

                return list(self.a.keys())

        def getvalues(self):

            if self.notdict():

                return list(self.a.values())

        def notdict(self):

            if type(self.a) != dict:

                raise Exception(self,a,'not a dictionary')

            return 1

        def userinput(self):

            self.a = eval(input())

            print(self.a,type(self.a))

            print(self.getykeys())

            print(self.getvalyes())

        def insertion(self,k,v):

            self.a[k]=v

d = dict_parsing({"k1":"amit", "k2":[1,2,3,4,5]})

d.getkeys()

Output:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-c26cd169473f> in <module>
----> 1 d.getkeys()

AttributeError: 'dict_parsing' object has no attribute 'getkeys'

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-c26cd169473f> in <module>
----> 1 d.getkeys()
AttributeError: 'dict_parsing' object has no attribute 'getkeys'

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.

Python3

class Geeks():

    def __init__(self):

        self.a = 'GeeksforGeeks'

obj = Geeks()

try:

    print(obj.a)

    print(obj.b)

except AttributeError:

    print("There is no such attribute")

Output:

GeeksforGeeks
There is no such attribute

Note: To know more about exception handling click here.

In every programming language, if we develop new programs, there is a high chance of getting errors or exceptions. These errors yield to the program not being executed. One of the error in Python mostly occurs is “AttributeError”. AttributeError can be defined as an error that is raised when an attribute reference or assignment fails. 
For example, if we take a variable x we are assigned a value of 10. In this process suppose we want to append another value to that variable. It’s not possible. Because the variable is an integer type it does not support the append method. So in this type of problem, we get an error called “AttributeError”. 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.
There are a few chances of getting AttributeError.
Example 1:

Python3

Output:

Traceback (most recent call last):
  File "/home/46576cfdd7cb1db75480a8653e2115cc.py", line 5, in 
    X.append(6)
AttributeError: 'int' object has no attribute 'append'

Example 2: Sometimes any variation in spelling will cause an Attribute error as Python is a case-sensitive language.

Python3

string = "The famous website is { }".fst("geeksforgeeks")

print(string)

Output:

Traceback (most recent call last):
  File "/home/2078367df38257e2ec3aead22841c153.py", line 3, in 
    string = "The famous website is { }".fst("geeksforgeeks")
AttributeError: 'str' object has no attribute 'fst'

Example 3: AttributeError can also be raised for a user-defined class when the user tries to make an invalid attribute reference.

Python3

class Geeks():

    def __init__(self):

        self.a = 'GeeksforGeeks'

obj = Geeks()

print(obj.a)

print(obj.b)

Output: 

GeeksforGeeks

Error:

Traceback (most recent call last):
  File "/home/373989a62f52a8b91cb2d3300f411083.py", line 17, in 
    print(obj.b)
AttributeError: 'Geeks' object has no attribute 'b'

Example 4:  AttributeError can also be raised for a user-defined class when the user misses out on adding tabs or spaces between their lines of code.

Python3

class dict_parsing:

    def __init__(self,a):

        self.a = a

        def getkeys(self):

            if self.notdict():

                return list(self.a.keys())

        def getvalues(self):

            if self.notdict():

                return list(self.a.values())

        def notdict(self):

            if type(self.a) != dict:

                raise Exception(self,a,'not a dictionary')

            return 1

        def userinput(self):

            self.a = eval(input())

            print(self.a,type(self.a))

            print(self.getykeys())

            print(self.getvalyes())

        def insertion(self,k,v):

            self.a[k]=v

d = dict_parsing({"k1":"amit", "k2":[1,2,3,4,5]})

d.getkeys()

Output:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-c26cd169473f> in <module>
----> 1 d.getkeys()

AttributeError: 'dict_parsing' object has no attribute 'getkeys'

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-c26cd169473f> in <module>
----> 1 d.getkeys()
AttributeError: 'dict_parsing' object has no attribute 'getkeys'

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.

Python3

class Geeks():

    def __init__(self):

        self.a = 'GeeksforGeeks'

obj = Geeks()

try:

    print(obj.a)

    print(obj.b)

except AttributeError:

    print("There is no such attribute")

Output:

GeeksforGeeks
There is no such attribute

Note: To know more about exception handling click here.

Errors are an essential part of a programmer’s life. And it is not at all bad if you get an error. Getting error means you are learning something new. But we need to solve those errors. And before solving that error, we should know why we are getting that error. There are some commonly occurred errors in python like Type Error, Syntax Error, Key Error, Attribute error, Name Error, and so on.  

In this article, we will learn about what is python Attribute Error, why we get it, and how we resolve it? Python interpreter raises an Attribute Error when we try to call or access an attribute of an object, but that object does not possess that attribute. For example- If we try using upper() on an integer, we will get an attribute error.  

Why we Get Attribute Error? 

Whenever we try to access an attribute that is not possessed by that object, we get an attribute error. For example- We know that to make the string uppercase, we use the upper(). 

Output- 

AttributeError: 'int' object has no attribute 'upper' 

Here, we are trying to convert an integer to an upper case letter, which is not possible as integers do not attribute being upper or lower. But if try using this upper() on a string, we would have got a result because a string can be qualified as upper or lower.  

Some Common Mistakes which result in Attribute error in python 

If we try to perform append() on any data type other than List: 

Sometimes when we want to concatenate two strings we try appending one string into another, which is not possible and we get an Attribute Error. 

string1="Ashwini" 
string2="Mandani" 
string1.append(string2) 

Output- 

AttributeError: 'str' object has no attribute 'append' 

Same goes with tuples, 

a=tuple((5,6)) 
a.append(7) 

Output- 

AttributeError: 'tuple' object has no attribute 'append' 

Trying to access attribute of Class: 

Sometimes, what we do is that we try to access attributes of a class which it does not possess. Let us better understand it with an example. 

Here, we have two classes- One is Person class and the other is Vehicle class. Both possess different properties. 

class Person: 

   def __init__(self,age,gender,name): 
       self.age=age 
       self.gender=gender 
       self.name=name 

   def speak(self): 
        print("Hello!! How are you?") 

class Vehicle: 

   def __init__(self , model_type , engine_type): 
        self.model_type = model_type 
        self.engine_type = engine_type 

   def horn(self): 
        print("beep!! beep") 

ashwini=Person(20,"male","ashwini") 
print(ashwini.gender) 
print(ashwini.engine_type) 

Output- 

male  
AttributeError: 'Person' object has no attribute 'engine_type'  
AttributeError: 'Person' object has no attribute 'horn' 
car=Vehicle( "Hatchback" , "Petrol" ) 
print(car.engine_type) 
print(car.gender) 

Output- 

Petrol 
AttributeError: 'Vehicle' object has no attribute 'gender' 
Error-
AttributeError: 'Vehicle' object has no attribute 'speak' 

In the above examples, when we tried to access the gender property of Person Class, we were successful. But when we tried to access the engine_type() attribute, it showed us an error. It is because a Person has no attribute called engine_type. Similarly, when we tried calling engine_type on Vehicle, we were successful, but that was not in the case of gender, as Vehicle has no attribute called gender. 

AttributeError: ‘NoneType’

We get NoneType Error when we get ‘None’ instead of the instance we are supposing we will get. It means that an assignment failed or returned an unexpected result.

name=None
i=5
if i%2==0:
    name="ashwini"
name.upper()

Output-

AttributeError: 'NoneType' object has no attribute 'upper'

While working with Modules:

It is very common to encounter an attribute error while working with modules. Suppose, we are importing a module named hello and trying to access two functions in it. One is print_name() and another is print_age().

Module Hello-

def print_name():
    print("Hello! The name of this module is module1")

import hello

hello.print_name()
hello.print_age()

Output-

Hello! The name of this module is module1

AttributeError: module 'hello' has no attribute 'print_age'

As the module hello does not contain print_age attribute, we got an Attribute error. In the next section, we will learn how to resolve this error.

How to Resolve Attribute Error in Python 

Use help():

The developers of python have tried to solve any possible problem faced by Python programmers. In this case, too, if we are getting confused, that whether a particular attribute belongs to an object or not, we can make use of help(). For example, if we don’t know whether we can use append() on a string, we can print(help(str)) to know all the operations that we can perform on strings. Not only these built-in data types, but we can also use help() on user-defined data types like Class.  

For example- if we don’t know what attributes does class Person that we declared above has,  

print(help(Person)) 

Output- 

python attribute error

Isn’t it great! These are precisely the attributes we defined in our Person class. 

Now, let us try using help() on our hello module inside the hi module.

Help on module hello:
NAME
hello
FUNCTIONS
print_name()

Using Try – Except Statement 

A very professional way to tackle not only Attribute error but any error is by using try-except statements. If we think we might get an error in a particular block of code, we can enclose them in a try block. Let us see how to do this. 

Suppose, we are not sure whether Person class contain engine_type attribute or not, we can enclose it in try block. 

class Vehicle: 

   def __init__(self , model_type , engine_type): 
        self.model_type = model_type 
        self.engine_type = engine_type 

   def horn(self): 
        print("beep!! beep") 

car=Vehicle( "Hatchback" , "Petrol" ) 

try: 
   print(car.engine_type) 
   print(car.gender) 

except Exception as e: 
   print(e)  

Output- 

Petrol 
'Vehicle' object has no attribute 'gender'. 

Must Read

  • How to Convert String to Lowercase in
  • How to Calculate Square Root
  • User Input | Input () Function | Keyboard Input
  • Best Book to Learn Python

Conclusion 

Whenever to try to access an attribute of an object that does not belong to it, we get an Attribute Error in Python. We can tackle it using either help() function or try-except statements. 

Try to run the programs on your side and let us know if you have any queries.

Happy Coding!

The Python AttributeError is an exception that occurs when an attribute reference or assignment fails. This can occur when an attempt is made to reference an attribute on a value that does not support the attribute.

What Causes AttributeError

The Python AttributeError is raised when an invalid attribute reference is made. This can happen if an attribute or function not associated with a data type is referenced on it. For example, if a method is called on an integer value, an AttributeError is raised.

Python AttributeError Example

Here’s an example of a Python AttributeError raised when trying call a method on an integer:

i = 1
i.append(2)

In the above example, a method is attempted to be called on an integer. Since integers in Python do not support any methods, running the above code raises a AttributeError:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    i.append(2)
AttributeError: 'int' object has no attribute 'append'

How to Fix AttributeError in Python

To avoid the AttributeError in Python code, a check should be performed before referencing an attribute on an object to ensure that it exists. The Python help() function can be used to find out all attributes and methods related to the object.

To resolve the AttributeError, a try-except block can be used. The lines of code that can throw the AttributeError should be placed in the try block, and the except block can catch and handle the error.

Using the above approach, the previous example can be updated to handle the error:

i = 1
try:
    i.append(2)
except AttributeError:
    print('No such attribute')>
    

Here, a check is performed for the AttributeError using the try-except block. When the above code is executed, the except block catches the AttributeError and handles it, producing the following output:

No such attribute

Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Python errors easier than ever. Try it today!

Attributes are the properties and methods defined for a class, object, or data type. In Python, everything is an object. That’s why many inbuilt data types, such as list, tuple, int, float, etc., support methods and properties.

Different objects have different attribute values, for instance, the list support

append()

method, whereas the tuple does not. And if we try to call the

append()

method on a tuple object, we will receive the AttributeError with an error message that this tuple object does not have the following attribute.

This is not only with tuples and lists. For every object, whether it is an object of a user-defined class or an inbuilt data type, if we try to access an unsupported attribute(property or method) on that object, we will encounter the AttributeError.

In this Python tutorial, we will discuss what AttributeError is and why it occurs in a Python program.

AttributeError is one of Python’s standard exceptions. And as a Python developer, you will encounter this error many times. But once you know why this error is raised in your program, you will be able to solve it in no time. Before we discuss the AttributeError, let’s take a look at what an attribute is.


What are Attributes?

Attributes are the properties and methods that are defined for a class or object. When we create a class using the

class

keywords, all the properties and methods defined inside that class will be called the class attributes. And once we define an object for that class, we can access those attributes using that object only. Similarly, built-in objects like a list, tuple, dictionary, int, float, etc., come with built-in attributes. We can list out all the supported attributes of an object using the dir() function.


Example

# all inbuilt attributes of list objects

x = [1,3,5,7,9]

print("List attributes")
print(dir(x))


Output

List attributes
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', 
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', 
'__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', 
'__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__',
 '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 
'remove', 'reverse', 'sort'
]


AttributeError

To access an attribute of an object, we use the object name followed by the dot operator and the attribute name. If the attribute is a method, we also put the parenthesis »


()


» after the attribute name. But if we try to access such an attribute that does not exist for that object, we will receive the attribute Error.


Example 1: AttributeError with built-in Objects

Let’s say we have two containers

container1

is a list object and

container2

is a set object. We also have a string of which words we need to add to these two containers. As

container1

is a list it can contain duplicate words but the

container2

will only contain unique words.

string = "As the years pass by, we all know owners look more and more like their dogs"

#list container
container1 = list()

#set container
container2 = set()

for word in string.split():
    container1.append(word)    #add word to the list
    container2.append(word)    #add word to the set (error)

print("List container: ", container1)
print("Set container: ", container2)


Output

Traceback (most recent call last):
  File "main.py", line 11, in 
    container2.append(word)    #add word to the set (error)
AttributeError: 'set' object has no attribute 'append'

In this example, we are getting the attribute error


"AttributeError: 'set' object has no attribute 'append'"

.

This is because container2 is a

set

object, and it does not have any method

append()

. append() method is only supported by Python’s list objects. To add a new element to the set object, we use the

add()

method.


Example 1 solution

string = "As the years pass by, we all know owners look more and more like their dogs"

#list container
container1 = list()

#set container
container2 = set()

for word in string.split():
    container1.append(word)    #add word to the list
    container2.add(word)    #add word to the set 

print("List container: ", container1)
print("Set container: ", container2)


Output

List container: ['As', 'the', 'years', 'pass', 'by,', 'we', 'all', 'know', 'owners', 'look', 'more', 'and', 'more', 'like', 'their', 'dogs']
Set container: {'pass', 'like', 'the', 'we', 'know', 'years', 'and', 'by,', 'their', 'dogs', 'look', 'owners', 'As', 'all', 'more'}


Example 2: AttributeError class and objects

We also the attribute error when we try to access such property or method of a class that does not exist. This generally happens when we make some typos while calling a property or method.

class Order:
    def __init__(self, product, name):
        self.product = product
        self.name = name

    def price(self):
        if self.product == "coffee":
            return 200
        if self.product == "tea":
            return 100

order1 = Order("coffee", "Raj")

print("Bill: ", order1.bill())


Output

Traceback (most recent call last):
  File "main.py", line 14, in 
    print("Bill: ", order1.bill())
AttributeError: 'Order' object has no attribute 'bill'

In the above example, we are getting this error because we are trying to access the

bill()

method that is not defined in the

Order

class. To solve this problem, we need to ensure that we are calling the correct method to access the price for our product. In our Order class, to get the total price, we have the

price()

method not

bill()

.


Example 2 solution

class Order:
    def __init__(self, product, name):
        self.product = product
        self.name = name

    def price(self):
        if self.product == "coffee":
            return 200
        if self.product == "tea":
            return 100

order1 = Order("coffee", "Raj")

print("Bill: ", order1.price())


Output

Bill: 200


Conclusion

Attribute Error is raised in a Python program when we try to access an unsupported attribute using an object. To solve the error, we need to ensure that the method or property we are trying to access through the object does support the object.

If you are getting this error with some built-in data type object and do not know all the attributes of the object. For that, you can use the Python dir() function, which will list all the attributes supported by that particular object.


People are also reading:

  • Python IndentationError: expected an indented block Solution

  • How to use Gmail API in python to send mail?

  • Python TypeError: Method_Name() missing 1 required positional argument: ‘self’ Solution

  • Install python package using jupyter notebook

  • Python TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’ Solution

  • How to extract all stored chrome password with Python

  • Python TypeError: can only join an iterable Solution

  • How to automate login using selenium in Python

  • Python TypeError: ‘float’ object is not callable Solution

  • Your guide for starting python coding on a MacBook

Python выводит трассировку (далее traceback), когда в вашем коде появляется ошибка. Вывод traceback может быть немного пугающим, если вы видите его впервые, или не понимаете, чего от вас хотят. Однако traceback Python содержит много информации, которая может помочь вам определить и исправить причину, из-за которой в вашем коде возникла ошибка.

Содержание статьи

  • Traceback — Что это такое и почему оно появляется?
  • Как правильно читать трассировку?
  • Обзор трассировка Python
  • Подробный обзор трассировки в Python
  • Обзор основных Traceback исключений в Python
  • AttributeError
  • ImportError
  • IndexError
  • KeyError
  • NameError
  • SyntaxError
  • TypeError
  • ValueError
  • Логирование ошибок из Traceback
  • Вывод

Понимание того, какую информацию предоставляет traceback Python является основополагающим критерием того, как стать лучшим Python программистом.

К концу данной статьи вы сможете:

  • Понимать, что несет за собой traceback
  • Различать основные виды traceback
  • Успешно вести журнал traceback, при этом исправить ошибку

Python Traceback — Как правильно читать трассировку?

Traceback (трассировка) — это отчет, который содержит вызовы выполненных функций в вашем коде в определенный момент.

Есть вопросы по Python?

На нашем форуме вы можете задать любой вопрос и получить ответ от всего нашего сообщества!

Telegram Чат & Канал

Вступите в наш дружный чат по Python и начните общение с единомышленниками! Станьте частью большого сообщества!

Паблик VK

Одно из самых больших сообществ по Python в социальной сети ВК. Видео уроки и книги для вас!

Traceback называют по разному, иногда они упоминаются как трассировка стэка, обратная трассировка, и так далее. В Python используется определение “трассировка”.

Когда ваша программа выдает ошибку, Python выводит текущую трассировку, чтобы подсказать вам, что именно пошло не так. Ниже вы увидите пример, демонстрирующий данную ситуацию:

def say_hello(man):

    print(‘Привет, ‘ + wrong_variable)

say_hello(‘Иван’)

Здесь say_hello() вызывается с параметром man. Однако, в say_hello() это имя переменной не используется. Это связано с тем, что оно написано по другому: wrong_variable в вызове print().

Обратите внимание: в данной статье подразумевается, что вы уже имеете представление об ошибках Python. Если это вам не знакомо, или вы хотите освежить память, можете ознакомиться с нашей статьей: Обработка ошибок в Python

Когда вы запускаете эту программу, вы получите следующую трассировку:

Traceback (most recent call last):

  File «/home/test.py», line 4, in <module>

    say_hello(‘Иван’)

  File «/home/test.py», line 2, in say_hello

    print(‘Привет, ‘ + wrong_variable)

NameError: name ‘wrong_variable’ is not defined

Process finished with exit code 1

Эта выдача из traceback содержит массу информации, которая вам понадобится для определения проблемы. Последняя строка трассировки говорит нам, какой тип ошибки возник, а также дополнительная релевантная информация об ошибке. Предыдущие строки из traceback указывают на код, из-за которого возникла ошибка.

В traceback выше, ошибкой является NameError, она означает, что есть отсылка к какому-то имени (переменной, функции, класса), которое не было определено. В данном случае, ссылаются на имя wrong_variable.

Последняя строка содержит достаточно информации для того, чтобы вы могли решить эту проблему. Поиск переменной wrong_variable, и заменит её атрибутом из функции на man. Однако, скорее всего в реальном случае вы будете иметь дело с более сложным кодом.

Python Traceback — Как правильно понять в чем ошибка?

Трассировка Python содержит массу полезной информации, когда вам нужно определить причину ошибки, возникшей в вашем коде. В данном разделе, мы рассмотрим различные виды traceback, чтобы понять ключевые отличия информации, содержащейся в traceback.

Существует несколько секций для каждой трассировки Python, которые являются крайне важными. Диаграмма ниже описывает несколько частей:

Обзор трассировки Python

В Python лучше всего читать трассировку снизу вверх.

  1. Синее поле: последняя строка из traceback — это строка уведомления об ошибке. Синий фрагмент содержит название возникшей ошибки.
  2. Зеленое поле: после названия ошибки идет описание ошибки. Это описание обычно содержит полезную информацию для понимания причины возникновения ошибки.
  3. Желтое поле: чуть выше в трассировке содержатся различные вызовы функций. Снизу вверх — от самых последних, до самых первых. Эти вызовы представлены двухстрочными вводами для каждого вызова. Первая строка каждого вызова содержит такую информацию, как название файла, номер строки и название модуля. Все они указывают на то, где может быть найден код.
  4. Красное подчеркивание: вторая строка этих вызовов содержит непосредственный код, который был выполнен с ошибкой.

Есть ряд отличий между выдачей трассировок, когда вы запускает код в командной строке, и между запуском кода в REPL. Ниже вы можете видеть тот же код из предыдущего раздела, запущенного в REPL и итоговой выдачей трассировки:

Python 3.7.4 (default, Jul 16 2019, 07:12:58)

[GCC 9.1.0] on linux

Type «help», «copyright», «credits» or «license» for more information.

>>>

>>>

>>> def say_hello(man):

...     print(‘Привет, ‘ + wrong_variable)

...

>>> say_hello(‘Иван’)

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

  File «<stdin>», line 2, in say_hello

NameError: name ‘wrong_variable’ is not defined

Обратите внимание на то, что на месте названия файла вы увидите <stdin>. Это логично, так как вы выполнили код через стандартный ввод. Кроме этого, выполненные строки кода не отображаются в traceback.

Важно помнить: если вы привыкли видеть трассировки стэка в других языках программирования, то вы обратите внимание на явное различие с тем, как выглядит traceback в Python. Большая часть других языков программирования выводят ошибку в начале, и затем ведут сверху вниз, от недавних к последним вызовам.

Это уже обсуждалось, но все же: трассировки Python читаются снизу вверх. Это очень помогает, так как трассировка выводится в вашем терминале (или любым другим способом, которым вы читаете трассировку) и заканчивается в конце выдачи, что помогает последовательно структурировать прочтение из traceback и понять в чем ошибка.

Traceback в Python на примерах кода

Изучение отдельно взятой трассировки поможет вам лучше понять и увидеть, какая информация в ней вам дана и как её применить.

Код ниже используется в примерах для иллюстрации информации, данной в трассировке Python:

Мы запустили ниже предоставленный код в качестве примера и покажем какую информацию мы получили от трассировки.

Сохраняем данный код в файле greetings.py

def who_to_greet(person):

    return person if person else input(‘Кого приветствовать? ‘)

def greet(someone, greeting=‘Здравствуйте’):

    print(greeting + ‘, ‘ + who_to_greet(someone))

def greet_many(people):

    for person in people:

        try:

            greet(person)

        except Exception:

            print(‘Привет, ‘ + person)

Функция who_to_greet() принимает значение person и либо возвращает данное значение если оно не пустое, либо запрашивает  значение от пользовательского ввода через input().

Далее, greet() берет имя для приветствия из someone, необязательное значение из greeting и вызывает print(). Также с переданным значением из someone вызывается who_to_greet().

Наконец, greet_many() выполнит итерацию по списку людей и вызовет greet(). Если при вызове greet() возникает ошибка, то выводится резервное приветствие print('hi, ' + person).

Этот код написан правильно, так что никаких ошибок быть не может при наличии правильного ввода.

Если вы добавите вызов функции greet() в конце нашего кода (которого сохранили в файл greetings.py) и дадите аргумент который он не ожидает (например, greet('Chad', greting='Хай')), то вы получите следующую трассировку:

$ python greetings.py

Traceback (most recent call last):

  File «/home/greetings.py», line 19, in <module>

    greet(‘Chad’, greting=‘Yo’)

TypeError: greet() got an unexpected keyword argument ‘greting’

Еще раз, в случае с трассировкой Python, лучше анализировать снизу вверх. Начиная с последней строки трассировки, вы увидите, что ошибкой является TypeError. Сообщения, которые следуют за типом ошибки, дают вам полезную информацию. Трассировка сообщает, что greet() вызван с аргументом, который не ожидался. Неизвестное название аргумента предоставляется в том числе, в нашем случае это greting.

Поднимаясь выше, вы можете видеть строку, которая привела к исключению. В данном случае, это вызов greet(), который мы добавили в конце greetings.py.

Следующая строка дает нам путь к файлу, в котором лежит код, номер строки этого файла, где вы можете найти код, и то, какой в нем модуль. В нашем случае, так как наш код не содержит никаких модулей Python, мы увидим только надпись , означающую, что этот файл является выполняемым.

С другим файлом и другим вводом, вы можете увидеть, что трассировка явно указывает вам на правильное направление, чтобы найти проблему. Следуя этой информации, мы удаляем злополучный вызов greet() в конце greetings.py, и добавляем следующий файл под названием example.py в папку:

from greetings import greet

greet(1)

Здесь вы настраиваете еще один файл Python, который импортирует ваш предыдущий модуль greetings.py, и используете его greet(). Вот что произойдете, если вы запустите example.py:

$ python example.py

Traceback (most recent call last):

  File «/path/to/example.py», line 3, in <module>

    greet(1)

  File «/path/to/greetings.py», line 5, in greet

    print(greeting + ‘, ‘ + who_to_greet(someone))

TypeError: must be str, not int

В данном случае снова возникает ошибка TypeError, но на этот раз уведомление об ошибки не очень помогает. Оно говорит о том, что где-то в коде ожидается работа со строкой, но было дано целое число.

Идя выше, вы увидите строку кода, которая выполняется. Затем файл и номер строки кода. На этот раз мы получаем имя функции, которая была выполнена — greet().

Поднимаясь к следующей выполняемой строке кода, мы видим наш проблемный вызов greet(), передающий целое число.

Иногда, после появления ошибки, другой кусок кода берет эту ошибку и также её выдает. В таких случаях, Python выдает все трассировки ошибки в том порядке, в котором они были получены, и все по тому же принципу, заканчивая на самой последней трассировке.

Так как это может сбивать с толку, рассмотрим пример. Добавим вызов greet_many() в конце greetings.py:

# greetings.py

...

greet_many([‘Chad’, ‘Dan’, 1])

Это должно привести к выводу приветствия всем трем людям. Однако, если вы запустите этот код, вы увидите несколько трассировок в выдаче:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

$ python greetings.py

Hello, Chad

Hello, Dan

Traceback (most recent call last):

  File «greetings.py», line 10, in greet_many

    greet(person)

  File «greetings.py», line 5, in greet

    print(greeting + ‘, ‘ + who_to_greet(someone))

TypeError: must be str, not int

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File «greetings.py», line 14, in <module>

    greet_many([‘Chad’, ‘Dan’, 1])

  File «greetings.py», line 12, in greet_many

    print(‘hi, ‘ + person)

TypeError: must be str, not int

Обратите внимание на выделенную строку, начинающуюся с “During handling in the output above”. Между всеми трассировками, вы ее увидите.

Это достаточно ясное уведомление: Пока ваш код пытался обработать предыдущую ошибку, возникла новая.

Обратите внимание: функция отображения предыдущих трассировок была добавлена в Python 3. В Python 2 вы можете получать только трассировку последней ошибки.

Вы могли видеть предыдущую ошибку, когда вызывали greet() с целым числом. Так как мы добавили 1 в список людей для приветствия, мы можем ожидать тот же результат. Однако, функция greet_many() оборачивает вызов greet() и пытается в блоке try и except. На случай, если greet() приведет к ошибке, greet_many() захочет вывести приветствие по-умолчанию.

Соответствующая часть greetings.py повторяется здесь:

def greet_many(people):

    for person in people:

        try:

            greet(person)

        except Exception:

            print(‘hi, ‘ + person)

Когда greet() приводит к TypeError из-за неправильного ввода числа, greet_many() обрабатывает эту ошибку и пытается вывести простое приветствие. Здесь код приводит к другой, аналогичной ошибке. Он все еще пытается добавить строку и целое число.

Просмотр всей трассировки может помочь вам увидеть, что стало причиной ошибки. Иногда, когда вы получаете последнюю ошибку с последующей трассировкой, вы можете не увидеть, что пошло не так. В этих случаях, изучение предыдущих ошибок даст лучшее представление о корне проблемы.

Обзор основных Traceback исключений в Python 3

Понимание того, как читаются трассировки Python, когда ваша программа выдает ошибку, может быть очень полезным навыком, однако умение различать отдельные трассировки может заметно ускорить вашу работу.

Рассмотрим основные ошибки, с которыми вы можете сталкиваться, причины их появления и что они значат, а также информацию, которую вы можете найти в их трассировках.

Ошибка AttributeError object has no attribute [Решено]

AttributeError возникает тогда, когда вы пытаетесь получить доступ к атрибуту объекта, который не содержит определенного атрибута. Документация Python определяет, когда эта ошибка возникнет:

Возникает при вызове несуществующего атрибута или присвоение значения несуществующему атрибуту.

Пример ошибки AttributeError:

>>> an_int = 1

>>> an_int.an_attribute

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

AttributeError: ‘int’ object has no attribute ‘an_attribute’

Строка уведомления об ошибке для AttributeError говорит вам, что определенный тип объекта, в данном случае int, не имеет доступа к атрибуту, в нашем случае an_attribute. Увидев AttributeError в строке уведомления об ошибке, вы можете быстро определить, к какому атрибуту вы пытались получить доступ, и куда перейти, чтобы это исправить.

Большую часть времени, получение этой ошибки определяет, что вы возможно работаете с объектом, тип которого не является ожидаемым:

>>> a_list = (1, 2)

>>> a_list.append(3)

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

AttributeError: ‘tuple’ object has no attribute ‘append’

В примере выше, вы можете ожидать, что a_list будет типом списка, который содержит метод .append(). Когда вы получаете ошибку AttributeError, и видите, что она возникла при попытке вызова .append(), это говорит о том, что вы, возможно, не работаете с типом объекта, который ожидаете.

Часто это происходит тогда, когда вы ожидаете, что объект вернется из вызова функции или метода и будет принадлежать к определенному типу, но вы получаете тип объекта None. В данном случае, строка уведомления об ошибке будет выглядеть так:

AttributeError: ‘NoneType’ object has no attribute ‘append’

Python Ошибка ImportError: No module named [Решено]

ImportError возникает, когда что-то идет не так с оператором import. Вы получите эту ошибку, или ее подкласс ModuleNotFoundError, если модуль, который вы хотите импортировать, не может быть найден, или если вы пытаетесь импортировать что-то, чего не существует во взятом модуле. Документация Python определяет, когда возникает эта ошибка:

Ошибка появляется, когда в операторе импорта возникают проблемы при попытке загрузить модуль. Также вызывается, при конструкции импорта from list в from ... import имеет имя, которое невозможно найти.

Вот пример появления ImportError и ModuleNotFoundError:

>>> import asdf

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

ModuleNotFoundError: No module named ‘asdf’

>>> from collections import asdf

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

ImportError: cannot import name ‘asdf’

В примере выше, вы можете видеть, что попытка импорта модуля asdf, который не существует, приводит к ModuleNotFoundError. При попытке импорта того, что не существует (в нашем случае — asdf) из модуля, который существует (в нашем случае — collections), приводит к ImportError. Строки сообщения об ошибке трассировок указывают на то, какая вещь не может быть импортирована, в обоих случаях это asdf.

Ошибка IndexError: list index out of range [Решено]

IndexError возникает тогда, когда вы пытаетесь вернуть индекс из последовательности, такой как список или кортеж, и при этом индекс не может быть найден в последовательности. Документация Python определяет, где эта ошибка появляется:

Возникает, когда индекс последовательности находится вне диапазона.

Вот пример, который приводит к IndexError:

>>> a_list = [‘a’, ‘b’]

>>> a_list[3]

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

IndexError: list index out of range

Строка сообщения об ошибке для IndexError не дает вам полную информацию. Вы можете видеть, что у вас есть отсылка к последовательности, которая не доступна и то, какой тип последовательности рассматривается, в данном случае это список.

Иными словами, в списке a_list нет значения с ключом 3. Есть только значение с ключами 0 и 1, это a и b соответственно.

Эта информация, в сочетании с остальной трассировкой, обычно является исчерпывающей для помощи программисту в быстром решении проблемы.

Возникает ошибка KeyError в Python 3 [Решено]

Как и в случае с IndexError, KeyError возникает, когда вы пытаетесь получить доступ к ключу, который отсутствует в отображении, как правило, это dict. Вы можете рассматривать его как IndexError, но для словарей. Из документации:

Возникает, когда ключ словаря не найден в наборе существующих ключей.

Вот пример появления ошибки KeyError:

>>> a_dict = [‘a’: 1, ‘w’: ‘2’]

>>> a_dict[‘b’]

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

KeyError: ‘b’

Строка уведомления об ошибки KeyError говорит о ключе, который не может быть найден. Этого не то чтобы достаточно, но, если взять остальную часть трассировки, то у вас будет достаточно информации для решения проблемы.

Ошибка NameError: name is not defined в Python [Решено]

NameError возникает, когда вы ссылаетесь на название переменной, модуля, класса, функции, и прочего, которое не определено в вашем коде.

Документация Python дает понять, когда возникает эта ошибка NameError:

Возникает, когда локальное или глобальное название не было найдено.

В коде ниже, greet() берет параметр person. Но в самой функции, этот параметр был назван с ошибкой, persn:

>>> def greet(person):

...     print(f‘Hello, {persn}’)

>>> greet(‘World’)

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

  File «<stdin>», line 2, in greet

NameError: name ‘persn’ is not defined

Строка уведомления об ошибке трассировки NameError указывает вам на название, которое мы ищем. В примере выше, это названная с ошибкой переменная или параметр функции, которые были ей переданы.

NameError также возникнет, если берется параметр, который мы назвали неправильно:

>>> def greet(persn):

...     print(f‘Hello, {person}’)

>>> greet(‘World’)

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

  File «<stdin>», line 2, in greet

NameError: name ‘person’ is not defined

Здесь все выглядит так, будто вы сделали все правильно. Последняя строка, которая была выполнена, и на которую ссылается трассировка выглядит хорошо.

Если вы окажетесь в такой ситуации, то стоит пройтись по коду и найти, где переменная person была использована и определена. Так вы быстро увидите, что название параметра введено с ошибкой.

Ошибка SyntaxError: invalid syntax в Python [Решено]

Возникает, когда синтаксический анализатор обнаруживает синтаксическую ошибку.

Ниже, проблема заключается в отсутствии двоеточия, которое должно находиться в конце строки определения функции. В REPL Python, эта ошибка синтаксиса возникает сразу после нажатия Enter:

>>> def greet(person)

  File «<stdin>», line 1

    def greet(person)

                    ^

SyntaxError: invalid syntax

Строка уведомления об ошибке SyntaxError говорит вам только, что есть проблема с синтаксисом вашего кода. Просмотр строк выше укажет вам на строку с проблемой. Каретка ^ обычно указывает на проблемное место. В нашем случае, это отсутствие двоеточия в операторе def нашей функции.

Стоит отметить, что в случае с трассировками SyntaxError, привычная первая строка Tracebak (самый последний вызов) отсутствует. Это происходит из-за того, что SyntaxError возникает, когда Python пытается парсить ваш код, но строки фактически не выполняются.

Ошибка TypeError в Python 3 [Решено]

TypeError возникает, когда ваш код пытается сделать что-либо с объектом, который не может этого выполнить, например, попытка добавить строку в целое число, или вызвать len() для объекта, в котором не определена длина.

Ошибка возникает, когда операция или функция применяется к объекту неподходящего типа.

Рассмотрим несколько примеров того, когда возникает TypeError:

>>> 1 + ‘1’

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

>>> ‘1’ + 1

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

TypeError: must be str, not int

>>> len(1)

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

TypeError: object of type ‘int’ has no len()

Указанные выше примеры возникновения TypeError приводят к строке уведомления об ошибке с разными сообщениями. Каждое из них весьма точно информирует вас о том, что пошло не так.

В первых двух примерах мы пытаемся внести строки и целые числа вместе. Однако, они немного отличаются:

  • В первом примере мы пытаемся добавить str к int.
  • Во втором примере мы пытаемся добавить int к str.

Уведомления об ошибке указывают на эти различия.

Последний пример пытается вызвать len() для int. Сообщение об ошибке говорит нам, что мы не можем сделать это с int.

Возникла ошибка ValueError в Python 3 [Решено]

ValueError возникает тогда, когда значение объекта не является корректным. Мы можем рассматривать это как IndexError, которая возникает из-за того, что значение индекса находится вне рамок последовательности, только ValueError является более обобщенным случаем.

Возникает, когда операция или функция получает аргумент, который имеет правильный тип, но неправильное значение, и ситуация не описывается более детальной ошибкой, такой как IndexError.

Вот два примера возникновения ошибки ValueError:

>>> a, b, c = [1, 2]

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

ValueError: not enough values to unpack (expected 3, got 2)

>>> a, b = [1, 2, 3]

Traceback (most recent call last):

  File «<stdin>», line 1, in <module>

ValueError: too many values to unpack (expected 2)

Строка уведомления об ошибке ValueError в данных примерах говорит нам в точности, в чем заключается проблема со значениями:

  1. В первом примере, мы пытаемся распаковать слишком много значений. Строка уведомления об ошибке даже говорит нам, где именно ожидается распаковка трех значений, но получаются только два.
  2. Во втором примере, проблема в том, что мы получаем слишком много значений, при этом получаем недостаточно значений для распаковки.

Логирование ошибок из Traceback в Python 3

Получение ошибки, и ее итоговой трассировки указывает на то, что вам нужно предпринять для решения проблемы. Обычно, отладка кода — это первый шаг, но иногда проблема заключается в неожиданном, или некорректном вводе. Хотя важно предусматривать такие ситуации, иногда есть смысл скрывать или игнорировать ошибку путем логирования traceback.

Рассмотрим жизненный пример кода, в котором нужно заглушить трассировки Python. В этом примере используется библиотека requests.

Файл urlcaller.py:

import sys

import requests

response = requests.get(sys.argv[1])

print(response.status_code, response.content)

Этот код работает исправно. Когда вы запускаете этот скрипт, задавая ему URL в качестве аргумента командной строки, он откроет данный URL, и затем выведет HTTP статус кода и содержимое страницы (content) из response. Это работает даже в случае, если ответом является статус ошибки HTTP:

$ python urlcaller.py https://httpbin.org/status/200

200 b»

$ python urlcaller.py https://httpbin.org/status/500

500 b»

Однако, иногда данный URL не существует (ошибка 404 — страница не найдена), или сервер не работает. В таких случаях, этот скрипт приводит к ошибке ConnectionError и выводит трассировку:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

$ python urlcaller.py http://thisurlprobablydoesntexist.com

...

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File «urlcaller.py», line 5, in <module>

    response = requests.get(sys.argv[1])

  File «/path/to/requests/api.py», line 75, in get

    return request(‘get’, url, params=params, **kwargs)

  File «/path/to/requests/api.py», line 60, in request

    return session.request(method=method, url=url, **kwargs)

  File «/path/to/requests/sessions.py», line 533, in request

    resp = self.send(prep, **send_kwargs)

  File «/path/to/requests/sessions.py», line 646, in send

    r = adapter.send(request, **kwargs)

  File «/path/to/requests/adapters.py», line 516, in send

    raise ConnectionError(e, request=request)

requests.exceptions.ConnectionError: HTTPConnectionPool(host=‘thisurlprobablydoesntexist.com’, port=80): Max retries exceeded with url: / (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x7faf9d671860>: Failed to establish a new connection: [Errno -2] Name or service not known’,))

Трассировка Python в данном случае может быть очень длинной, и включать в себя множество других ошибок, которые в итоге приводят к ошибке ConnectionError. Если вы перейдете к трассировке последних ошибок, вы заметите, что все проблемы в коде начались на пятой строке файла urlcaller.py.

Если вы обернёте неправильную строку в блоке try и except, вы сможете найти нужную ошибку, которая позволит вашему скрипту работать с большим числом вводов:

Файл urlcaller.py:

try:

    response = requests.get(sys.argv[1])

except requests.exceptions.ConnectionError:

    print(1, ‘Connection Error’)

else:

    print(response.status_code, response.content)

Код выше использует предложение else с блоком except.

Теперь, когда вы запускаете скрипт на URL, который приводит к ошибке ConnectionError, вы получите -1 в статусе кода и содержимое ошибки подключения:

$ python urlcaller.py http://thisurlprobablydoesntexist.com

1 Connection Error

Это работает отлично. Однако, в более реалистичных системах, вам не захочется просто игнорировать ошибку и итоговую трассировку, вам скорее понадобиться внести в журнал. Ведение журнала трассировок позволит вам лучше понять, что идет не так в ваших программах.

Обратите внимание: Для более лучшего представления о системе логирования в Python вы можете ознакомиться с данным руководством тут: Логирование в Python

Вы можете вести журнал трассировки в скрипте, импортировав пакет logging, получить logger, вызвать .exception() для этого логгера в куске except блока try и except. Конечный скрипт будет выглядеть примерно так:

# urlcaller.py

import logging

import sys

import requests

logger = logging.getLogger(__name__)

try:

    response = requests.get(sys.argv[1])

except requests.exceptions.ConnectionError as e:

    logger.exception()

    print(1, ‘Connection Error’)

else:

    print(response.status_code, response.content)

Теперь, когда вы запускаете скрипт с проблемным URL, он будет выводить исключенные -1 и ConnectionError, но также будет вести журнал трассировки:

$ python urlcaller.py http://thisurlprobablydoesntexist.com

...

  File «/path/to/requests/adapters.py», line 516, in send

    raise ConnectionError(e, request=request)

requests.exceptions.ConnectionError: HTTPConnectionPool(host=‘thisurlprobablydoesntexist.com’, port=80): Max retries exceeded with url: / (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x7faf9d671860>: Failed to establish a new connection: [Errno -2] Name or service not known’,))

1 Connection Error

По умолчанию, Python будет выводить ошибки в стандартный stderr. Выглядит так, будто мы совсем не подавили вывод трассировки. Однако, если вы выполните еще один вызов при перенаправлении stderr, вы увидите, что система ведения журналов работает, и мы можем изучать логи программы без необходимости личного присутствия во время появления ошибок:

$ python urlcaller.py http://thisurlprobablydoesntexist.com 2> mylogs.log

1 Connection Error

Подведем итоги данного обучающего материала

Трассировка Python содержит замечательную информацию, которая может помочь вам понять, что идет не так с вашим кодом Python. Эти трассировки могут выглядеть немного запутанно, но как только вы поймете что к чему, и увидите, что они в себе несут, они могут быть предельно полезными. Изучив несколько трассировок, строку за строкой, вы получите лучшее представление о предоставляемой информации.

Понимание содержимого трассировки Python, когда вы запускаете ваш код может быть ключом к улучшению вашего кода. Это способ, которым Python пытается вам помочь.

Теперь, когда вы знаете как читать трассировку Python, вы можете выиграть от изучения ряда инструментов и техник для диагностики проблемы, о которой вам сообщает трассировка. Модуль traceback может быть полезным, если вам нужно узнать больше из выдачи трассировки.

  • Текст является переводом статьи: Understanding the Python Traceback
  • Изображение из шапки статьи принадлежит сайту © Real Python

Являюсь администратором нескольких порталов по обучению языков программирования Python, Golang и Kotlin. В составе небольшой команды единомышленников, мы занимаемся популяризацией языков программирования на русскоязычную аудиторию. Большая часть статей была адаптирована нами на русский язык и распространяется бесплатно.

E-mail: vasile.buldumac@ati.utm.md

Образование
Universitatea Tehnică a Moldovei (utm.md)

  • 2014 — 2018 Технический Университет Молдовы, ИТ-Инженер. Тема дипломной работы «Автоматизация покупки и продажи криптовалюты используя технический анализ»
  • 2018 — 2020 Технический Университет Молдовы, Магистр, Магистерская диссертация «Идентификация человека в киберпространстве по фотографии лица»

Attributes are values or functions associated with an object, a data type, or a class. If you call an attribute on a value whose data type or class does not support that attribute, you’ll encounter an AttributeError.

This guide discusses what an AttributeError is and what it means. We’ll walk through an example of an AttributeError so you can learn how to fix one in your code.

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest

First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

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

In a Python class, you can define methods and values that are shared by the objects of that class. This is why some people think of classes as blueprints for objects.

Calling a method or a class is another way of saying you are referencing an attribute of that class. One way to think about an attribute is like a physical attribute of a person. Some people have blue eyes. Some people have pink-dyed hair. These are all attributes.

In a Python class, an attribute could be “eye_color”. This attribute could define the color of a person’s eyes. An attribute could also be a function. A function called changeEyeColor() could change the value of “eye_color”.

Data types have attributes. For instance, you can use the Python join() method to convert a string into a list. String objects support the join() method.

If you try to reference a function or a value not associated with a class object or a data type, you’ll encounter an AttributeError.

Attribute Error Python Example

Let’s write a program that merges two lists of shoes. Two shoe stores are going through a merger and want to make a list of all the unique shoes they sell.

To start, let’s define a Python set that contains the shoes from store one, Harrisons Shoes:

harrisons_shoes = {"Nike Air Force 1 07", "Nike Air Max Plus", "Adidas Gazelle"}

We use curly braces to define our set. Next, let’s define a set with the names of the shoes offered by the store that is merging with Harrisons. This shoe store is called the Shoe Emporium:

shoe_emporium = {"Adidas Samba", "Adidas Superstar", "Nike Air Max Plus"}

Because these two collections are sets, they can only store unique values. That means when we add them together, we’ll get a set with no duplicate values.

To add our sets together, we’re going to use the built-in function called extend():

harrisons_shoes.extend(shoe_emporium)
print(harrisons_shoes)

The extend() method adds all the shoes from the “shoe_emporium” set to the “harrisons_shoes” set. We use a Python print() statement. This lets us we can see all the shoes in our new set. Let’s run our code and see what happens:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
	harrisons_shoes.extend(shoe_emporium)
AttributeError: 'set' object has no attribute 'extend'

Our code returns an AttributeError.

AttributeError Python Solution

Our error message tells us we cannot use the method extend() on an object whose data type is a set. This is because extend() is a list method. It is not supported by sets.

If we want to merge our two sets, we have to use an addition sign:

harrisons_shoes.update(shoe_emporium)
print(harrisons_shoes)

This will add the contents of the “shoe_emporium” set to the “harrisons_shoes” set. We then print all the values in the “harrisons_shoes” set to the console. Let’s run our new program:

{'Nike Air Force 1 07', 'Adidas Superstar', 'Adidas Samba', 'Nike Air Max Plus', 'Adidas Gazelle'}

Our program returns a set with all the shoes from our two original sets. While there were six values in our original two sets, now there are only five. This is because two of the shoes were the same and sets can only store unique values.

Our program returns a set with all the shoes from our two original sets. While there were six values in our original two sets, now there are only five. This is because two of the shoes were the same and sets can only store unique values.

Similar AttributeErrors to Explore

AttributeErrors are incredibly common. They can arise when you try to call attributes of data types and classes that do not support the attribute you are referring to.

These errors may also be caused if you make a typo when referring to an attribute. Python will interpret your code as-is. If you make a typo, it will appear to Python that you are referring to an attribute that does not exist.

For instance, using the Python split() method to split a list is common. But, split() is a string method and so it cannot be used to split a list.

For further reading, consider researching the following errors:

  • AttributeError: ‘list’ object has no attribute ‘split’
  • AttributeError: ‘module’ object has no attribute ‘urlopen’

Conclusion

Attribute errors in Python are raised when an invalid attribute is referenced. To solve these errors, first check that the attribute you are calling exists. Then, make sure the attribute is related to the object or data type with which you are working.

If the attribute you want is associated with a built-in type and does not exist, you should look for an alternative. There are alternatives for many attributes that exist for one data type you can use on another data type. For instance, there is no extend() method with sets but you can use union() to join to sets.

To learn more about writing Python code, read our How to Learn Python guide.

Venus profile photo

«Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!»

Venus, Software Engineer at Rockbot

Автор оригинала: Team Python Pool.

Ошибки – неотъемлемая часть жизни программиста. И это совсем не плохо, если вы получаете ошибку. Получение ошибки означает, что вы изучаете что-то новое. Но мы должны устранить эти ошибки. И прежде чем решить эту ошибку, мы должны знать, почему мы получаем эту ошибку. В python есть некоторые часто встречающиеся ошибки, такие как Ошибка типа , Синтаксическая ошибка, Ошибка ключа, Ошибка атрибута, Ошибка имени, и так далее.

В этой статье мы узнаем о том, что такое python AttributeError, почему мы его получаем и как его разрешаем? Интерпретатор Python вызывает AttributeError, когда мы пытаемся вызвать или получить доступ к атрибуту объекта, но этот объект не обладает этим атрибутом. Например, Если мы попытаемся использовать функцию upper() для целого числа, то получим ошибку атрибута.

Почему мы Получаем AttributeError?

Всякий раз, когда мы пытаемся получить доступ к атрибуту, который не принадлежит этому объекту, мы получаем attributeerror. Например, Мы знаем, что для того, чтобы сделать строку прописной, мы используем upper().

Выход-

AttributeError: 'int' object has no attribute 'upper'

Здесь мы пытаемся преобразовать целое число в заглавную букву, что невозможно, поскольку целые числа не приписывают быть верхними или нижними. Но если попытаться использовать эту функцию upper() для строки, мы получим результат, потому что строка может быть квалифицирована как верхняя или нижняя.

Если мы попытаемся выполнить append() для любого типа данных, отличного от List:

Иногда, когда мы хотим объединить две строки, мы пытаемся добавить одну строку в другую, что невозможно, и мы получаем ошибку атрибута.

Выход-

AttributeError: 'str' object has no attribute 'append'

То же самое относится и к кортежам,

Выход-

AttributeError: 'tuple' object has no attribute 'append'

Попытка доступа к атрибуту класса:

Иногда мы пытаемся получить доступ к атрибутам класса, которыми он не обладает. Давайте лучше разберемся в этом на примере.

Здесь у нас есть два класса — один-класс человека, а другой — класс транспортного средства. Оба обладают разными свойствами.

class Person: 

   def __init__(self,age,gender,name): 
       
       
       

   def speak(self): 
        print("Hello!! How are you?") 

class Vehicle: 

   def __init__(self , model_type , engine_type): 
        
        

   def horn(self): 
        print("beep!! beep") 
(20,"male","ashwini") 
print(ashwini.gender) 
print(ashwini.engine_type)

Выход-

male  
AttributeError: 'Person' object has no attribute 'engine_type'
AttributeError: 'Person' object has no attribute 'horn'

Выход-

Petrol 
AttributeError: 'Vehicle' object has no attribute 'gender'
Error-
AttributeError: 'Vehicle' object has no attribute 'speak'

В приведенных выше примерах, когда мы попытались получить доступ к свойству пола класса Person, нам это удалось. Но когда мы попытались получить доступ к атрибуту engine_type (), он показал нам ошибку. Это происходит потому, что у человека нет атрибута под названием engine_type. Точно так же, когда мы попытались вызвать engine_type на транспортном средстве, мы добились успеха, но это было не в случае пола, так как Транспортное средство не имеет атрибута, называемого полом.

AttributeError: ‘NoneType’

Мы получаем ошибку NoneType, когда получаем «None» вместо экземпляра, который, как мы предполагаем, мы получим. Это означает, что задание провалилось или вернуло неожиданный результат.

Выход-

AttributeError: 'NoneType' object has no attribute 'upper'

При работе с модулями:

Очень часто при работе с модулями возникает ошибка атрибута. Предположим, мы импортируем модуль с именем hello и пытаемся получить доступ к двум функциям в нем. Один из них-print_name (), а другой-print_age().

Модуль Привет-

def print_name():
    print("Hello! The name of this module is module1")

import hello

hello.print_name()
hello.print_age()

Выход-

Hello! The name of this module is module1

AttributeError: module 'hello' has no attribute 'print_age'

Поскольку модуль hello не содержит атрибута print_age, мы получили атрибут Attributeerror. В следующем разделе мы узнаем, как устранить эту ошибку.

Как разрешить AttributeError в Python

Используйте справку():

Разработчики python пытались решить любую возможную проблему, с которой сталкиваются программисты Python. В этом случае также, если мы путаемся в том, принадлежит ли конкретный атрибут объекту или нет, мы можем использовать help(). Например, если мы не знаем, можем ли мы использовать append() для строки, мы можем print(help(str)) знать все операции, которые мы можем выполнять со строками. Не только эти встроенные типы данных, но мы также можем использовать help() для пользовательских типов данных, таких как Class.

Например, если мы не знаем, какими атрибутами обладает класс Person, объявленный нами выше,

Выход-

ошибка атрибута pythonошибка атрибута python

Разве это не здорово! Это именно те атрибуты, которые мы определили в нашем классе Персон.

Теперь давайте попробуем использовать help() для нашего модуля hello внутри модуля hi.

Help on module hello:
NAME
hello
FUNCTIONS
print_name()

Использование оператора Try – Except

Очень <сильный>профессиональный способ справиться не только с атрибутивной ошибкой, но и с любой ошибкой-это использовать try-except операторы. Если мы думаем, что можем получить ошибку в определенном блоке кода, мы можем заключить их в href=»https://en.wikipedia.org/wiki/Exception_handling»>попробуйте заблокировать. Давайте посмотрим, как это сделать. href=»https://en.wikipedia.org/wiki/Exception_handling»>попробуйте заблокировать. Давайте посмотрим, как это сделать.

Предположим, мы не уверены, содержит ли класс Person атрибут engine_type или нет, мы можем заключить его в блок try.

class Vehicle: 

   def __init__(self , model_type , engine_type): 
        
        

   def horn(self): 
        print("beep!! beep") 
( "Hatchback" , "Petrol" ) 

try: 
   print(car.engine_type) 
   print(car.gender) 

except Exception as e: 
   print(e)

Выход-

Petrol 
'Vehicle' object has no attribute 'gender'.

Должен Читать

  • Как преобразовать строку в нижний регистр в
  • Как вычислить Квадратный корень
  • Пользовательский ввод | Функция ввода () | Ввод с клавиатуры
  • Лучшая книга для изучения Python

Вывод

Всякий раз, когда мы пытаемся получить доступ к атрибуту объекта, который ему не принадлежит, мы получаем AttributeError в Python. Мы можем решить эту проблему с помощью функции help() или операторов try-except.

Попробуйте запустить программы на вашей стороне и дайте нам знать, если у вас есть какие-либо вопросы.

Счастливого кодирования!

Понравилась статья? Поделить с друзьями:
  • Atm code error
  • Atlas copco ошибки компрессора
  • Atlas copco ошибка конвертера
  • Atlas compass error tarrentella vs redanka remake
  • Atlant холодильник коды ошибок