Hướng dẫn traceback python - con trăn theo dõi



Các khóa học miễn phí qua video:
Lập trình C Java C# SQL Server PHP HTML5-CSS3-JavaScript

Mục lục bài viết:

  • Traceback Python là gì?
  • Làm thế nào để bạn đọc một Traceback Python?
    • Tổng quan về truy nguyên Python
    • Hướng dẫn theo dõi cụ thể
  • Một số dấu vết thường gặp trong Python là gì?
    • AttributeError
    • ImportError
    • IndexError
    • KeyError
    • NameError
    • Lỗi cú pháp
    • TypeError
    • ValueError
  • Làm thế nào để bạn ghi lại một dấu vết?
  • Phần kết luận

Show

Python in một dấu vết khi một ngoại lệ được đưa ra trong mã của bạn. Đầu ra theo dõi có thể hơi choáng ngợp nếu bạn nhìn thấy nó lần đầu tiên hoặc bạn không biết nó nói gì với bạn. Nhưng theo dõi Python có rất nhiều thông tin có thể giúp bạn chẩn đoán và khắc phục lý do ngoại lệ được nêu ra trong mã của bạn. Hiểu được thông tin mà một truy nguyên Python cung cấp là rất quan trọng để trở thành một lập trình viên Python tốt hơn.

Đến cuối hướng dẫn này, bạn sẽ có thể:

  • Tìm hiểu về lần theo dõi tiếp theo mà bạn thấy
  • Nhận ra một số dấu vết phổ biến hơn
  • Ghi lại thành công theo dõi trong khi vẫn xử lý ngoại lệ

Traceback Python là gì?

Làm thế nào để bạn đọc một Traceback Python?

Tổng quan về truy nguyên Python

# example.py
def greet(someone):
    print('Hello, ' + someon)

greet('Chad')

Hướng dẫn theo dõi cụ thể

Một số dấu vết thường gặp trong Python là gì?

AttributeError

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined

ImportError

IndexError

KeyError

Làm thế nào để bạn đọc một Traceback Python?

Tổng quan về truy nguyên Python

Tổng quan về truy nguyên Python

Hướng dẫn theo dõi cụ thể

Hướng dẫn traceback python - con trăn theo dõi

Một số dấu vết thường gặp trong Python là gì?

  1. AttributeError

  2. ImportError

  3. IndexError

  4. KeyError

NameError

Lỗi cú pháp

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined

TypeError

ValueError

Nó đã được nói rồi, nhưng chỉ cần nhắc lại, một truy nguyên Python nên được đọc từ dưới lên trên. Điều này rất hữu ích vì bản ghi lại được in ra và thiết bị đầu cuối của bạn (hoặc bất cứ nơi nào bạn đang đọc bản ghi lại) thường kết thúc ở cuối đầu ra, cho bạn một nơi hoàn hảo để bắt đầu đọc bản ghi lại.

Hướng dẫn theo dõi cụ thể

Xem qua một số kết quả theo dõi cụ thể sẽ giúp bạn hiểu rõ hơn và xem thông tin mà truy xuất nguồn gốc sẽ cung cấp cho bạn.

Đoạn mã dưới đây được sử dụng trong các ví dụ sau để minh họa thông tin mà truy nguyên Python cung cấp cho bạn:

# greetings.py
def who_to_greet(person):
    return person if person else input('Greet who? ')

def greet(someone, greeting='Hello'):
    print(greeting + ', ' + who_to_greet(someone))

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)

Ở đây, 

# greetings.py
def who_to_greet(person):
    return person if person else input('Greet who? ')

def greet(someone, greeting='Hello'):
    print(greeting + ', ' + who_to_greet(someone))

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
7nhận một giá trị 
# greetings.py
def who_to_greet(person):
    return person if person else input('Greet who? ')

def greet(someone, greeting='Hello'):
    print(greeting + ', ' + who_to_greet(someone))

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
8và trả về giá trị đó hoặc nhắc giá trị trả về thay thế.

Sau đó, 

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8lấy một cái tên để chào, 
>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
9một 
$ python example.py
Traceback (most recent call last):
  File "/path/to/greetings.py", line 19, in 
    greet('Chad', greting='Yo')
TypeError: greet() got an unexpected keyword argument 'greting'
1giá trị tùy chọn và gọi 
# greetings.py
def who_to_greet(person):
    return person if person else input('Greet who? ')

def greet(someone, greeting='Hello'):
    print(greeting + ', ' + who_to_greet(someone))

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
2. 
# greetings.py
def who_to_greet(person):
    return person if person else input('Greet who? ')

def greet(someone, greeting='Hello'):
    print(greeting + ', ' + who_to_greet(someone))

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
7cũng được gọi với 
>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
9giá trị được truyền vào.

Cuối cùng, 

$ python example.py
Traceback (most recent call last):
  File "/path/to/greetings.py", line 19, in 
    greet('Chad', greting='Yo')
TypeError: greet() got an unexpected keyword argument 'greting'
5sẽ lặp lại danh sách 
$ python example.py
Traceback (most recent call last):
  File "/path/to/greetings.py", line 19, in 
    greet('Chad', greting='Yo')
TypeError: greet() got an unexpected keyword argument 'greting'
6và gọi 
>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8. Nếu có một ngoại lệ được nêu ra bằng cách gọi 
>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8, thì một lời chào dự phòng đơn giản sẽ được in.

Mã này không có bất kỳ lỗi nào dẫn đến ngoại lệ được đưa ra miễn là đầu vào phù hợp được cung cấp.

Nếu bạn thêm một lệnh gọi vào 

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8cuối 
# example.py
from greetings import greet

greet(1)
0và chỉ định một đối số từ khóa mà nó không mong đợi (ví dụ 
# example.py
from greetings import greet

greet(1)
1), thì bạn sẽ nhận được dấu vết sau:

$ python example.py
Traceback (most recent call last):
  File "/path/to/greetings.py", line 19, in 
    greet('Chad', greting='Yo')
TypeError: greet() got an unexpected keyword argument 'greting'

Một lần nữa, với truy nguyên Python, tốt nhất là làm việc lùi lại, tăng đầu ra. Bắt đầu từ dòng cuối cùng của quá trình theo dõi, bạn có thể thấy rằng ngoại lệ là a 

# example.py
from greetings import greet

greet(1)
2. Các thông báo theo sau loại ngoại lệ, mọi thứ sau dấu hai chấm, cung cấp cho bạn một số thông tin tuyệt vời. Nó cho bạn biết rằng nó 
>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8đã được gọi với một đối số từ khóa mà nó không mong đợi. Tên tham số chưa biết cũng được trao cho bạn: 
# example.py
from greetings import greet

greet(1)
4.

Di chuyển lên trên, bạn có thể thấy dòng dẫn đến ngoại lệ. Trong trường hợp này, đó là lệnh 

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8gọi mà chúng tôi đã thêm vào cuối 
# example.py
from greetings import greet

greet(1)
0.

Dòng tiếp theo cung cấp cho bạn đường dẫn đến tệp có mã tồn tại, số dòng của tệp đó nơi mã có thể được tìm thấy và mô-đun đó nằm trong mô-đun nào. Trong trường hợp này, vì mã của chúng tôi không sử dụng bất kỳ mô-đun Python nào khác , chúng ta chỉ thấy 

# example.py
from greetings import greet

greet(1)
7ở đây, nghĩa là đây là tệp đang được thực thi.

Với một tệp khác và thông tin đầu vào khác, bạn có thể thấy bản truy xuất thực sự chỉ cho bạn đúng hướng để tìm ra vấn đề. Nếu bạn đang theo dõi, hãy xóa 

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8cuộc gọi lỗi ở cuối 
# example.py
from greetings import greet

greet(1)
0và thêm tệp sau vào thư mục của bạn:

# example.py
from greetings import greet

greet(1)

Tại đây, bạn đã thiết lập một tệp Python khác đang nhập mô-đun trước đó của bạn 

# example.py
from greetings import greet

greet(1)
0và sử dụng 
>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8từ nó. Đây là những gì sẽ xảy ra nếu bây giờ bạn chạy 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 3, in 
    greet(1)
  File "/path/to/greetings.py", line 5, in greet
    print(greeting + ', ' + who_to_greet(someone))
TypeError: must be str, not int
2:

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 3, in 
    greet(1)
  File "/path/to/greetings.py", line 5, in greet
    print(greeting + ', ' + who_to_greet(someone))
TypeError: must be str, not int

Ngoại lệ được nêu ra trong trường hợp này là một 

# example.py
from greetings import greet

greet(1)
2lần nữa, nhưng lần này thông báo ít hữu ích hơn một chút. Nó cho bạn biết rằng một nơi nào đó trong mã mà nó mong đợi hoạt động với một chuỗi, nhưng một số nguyên đã được đưa ra.

Di chuyển lên trên, bạn sẽ thấy dòng mã đã được thực thi. Sau đó, tệp và số dòng của mã. Tuy nhiên, lần này, thay vì 

# example.py
from greetings import greet

greet(1)
7, chúng ta nhận được tên của hàm đang được thực thi 
>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8,.

Chuyển đến dòng mã được thực thi tiếp theo, chúng tôi thấy 

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8lệnh gọi có vấn đề của chúng tôi được chuyển vào một số nguyên.

Đôi khi sau khi một ngoại lệ được nâng lên, một đoạn mã khác sẽ bắt được ngoại lệ đó và cũng dẫn đến một ngoại lệ. Trong những tình huống này, Python sẽ xuất ra tất cả các truy nguyên ngoại lệ theo thứ tự mà chúng đã được nhận, một lần nữa kết thúc trong lần truy xuất lại ngoại lệ nâng cao gần đây nhất.

Vì điều này có thể hơi khó hiểu, đây là một ví dụ. Thêm cuộc gọi vào 

$ python example.py
Traceback (most recent call last):
  File "/path/to/greetings.py", line 19, in 
    greet('Chad', greting='Yo')
TypeError: greet() got an unexpected keyword argument 'greting'
5cuối 
# example.py
from greetings import greet

greet(1)
0:

# greetings.py
...
greet_many(['Chad', 'Dan', 1])

Điều này sẽ dẫn đến việc in lời chúc mừng cho cả ba người. Tuy nhiên, nếu bạn chạy mã này, bạn sẽ thấy một ví dụ về nhiều dấu vết được xuất ra:

$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int

Lưu ý dòng được đánh dấu bắt đầu bằng 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 3, in 
    greet(1)
  File "/path/to/greetings.py", line 5, in greet
    print(greeting + ', ' + who_to_greet(someone))
TypeError: must be str, not int
9trong đầu ra ở trên. Ở giữa tất cả các lần theo dõi, bạn sẽ thấy dòng này. Thông điệp của nó rất rõ ràng, trong khi mã của bạn đang cố gắng xử lý ngoại lệ trước đó, một ngoại lệ khác đã được nêu ra.

Lưu ý : Tính năng hiển thị theo dõi ngoại lệ trước đó của Python đã được thêm vào trong Python 3. Trong Python 2, bạn sẽ chỉ nhận được dấu vết của ngoại lệ cuối cùng.

Bạn đã từng thấy ngoại lệ trước đó, khi bạn gọi 

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8với một số nguyên. Vì chúng tôi đã thêm một 
# greetings.py
...
greet_many(['Chad', 'Dan', 1])
1người vào danh sách những người cần chào hỏi, chúng tôi có thể mong đợi kết quả tương tự. Tuy nhiên, hàm 
$ python example.py
Traceback (most recent call last):
  File "/path/to/greetings.py", line 19, in 
    greet('Chad', greting='Yo')
TypeError: greet() got an unexpected keyword argument 'greting'
5kết thúc 
>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8cuộc gọi trong một 
# greetings.py
...
greet_many(['Chad', 'Dan', 1])
4và 
# greetings.py
...
greet_many(['Chad', 'Dan', 1])
5khối. Chỉ trong trường hợp 
>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8dẫn đến một ngoại lệ được nêu ra, 
$ python example.py
Traceback (most recent call last):
  File "/path/to/greetings.py", line 19, in 
    greet('Chad', greting='Yo')
TypeError: greet() got an unexpected keyword argument 'greting'
5muốn in một lời chào mặc định.

Phần liên quan của 

# example.py
from greetings import greet

greet(1)
0được lặp lại ở đây:

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)

Vì vậy, khi 

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8kết quả là 
# example.py
from greetings import greet

greet(1)
2do đầu vào số nguyên 
$ python example.py
Traceback (most recent call last):
  File "/path/to/greetings.py", line 19, in 
    greet('Chad', greting='Yo')
TypeError: greet() got an unexpected keyword argument 'greting'
5không hợp lệ , hãy xử lý ngoại lệ đó và cố gắng in một lời chào đơn giản. Ở đây mã kết thúc dẫn đến một ngoại lệ khác, tương tự, ngoại lệ. Nó vẫn đang cố gắng thêm một chuỗi và một số nguyên.

Xem tất cả đầu ra theo dõi có thể giúp bạn biết đâu có thể là nguyên nhân thực sự của một ngoại lệ. Đôi khi khi bạn thấy ngoại lệ cuối cùng được đưa ra và kết quả là quá trình truy nguyên của nó, bạn vẫn không thể thấy điều gì sai. Trong những trường hợp đó, chuyển sang các trường hợp ngoại lệ trước đó thường giúp bạn biết rõ hơn về nguyên nhân gốc rễ.

Một số dấu vết thường gặp trong Python là gì?

Biết cách đọc một truy nguyên Python khi chương trình của bạn tạo ra một ngoại lệ có thể rất hữu ích khi bạn đang lập trình, nhưng biết một số truy nguyên phổ biến hơn cũng có thể tăng tốc quá trình của bạn.

Dưới đây là một số trường hợp ngoại lệ phổ biến mà bạn có thể gặp phải, lý do chúng được nêu ra và ý nghĩa của chúng cũng như thông tin bạn có thể tìm thấy trong phần truy nguyên của chúng.

$ 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 greet_many(['Chad', 'Dan', 1]) File "greetings.py", line 12, in greet_many print('hi, ' + person) TypeError: must be str, not int 2

Giá trị 

$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
2này được nâng lên khi bạn cố gắng truy cập một thuộc tính trên một đối tượng không có thuộc tính đó được xác định. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra:

Được nâng lên khi một tham chiếu thuộc tính hoặc phép gán không thành công. (Nguồn)

Đây là một ví dụ về việc 

$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
2được nêu ra:

>>>

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
0

Dòng thông báo lỗi cho an 

$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
2cho bạn biết rằng loại đối tượng cụ thể, 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
6trong trường hợp này, không có thuộc tính được truy cập, 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
7trong trường hợp này. Nhìn thấy 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
2dòng thông báo lỗi có thể giúp bạn nhanh chóng xác định thuộc tính nào bạn đã cố gắng truy cập và nơi cần đến để sửa nó.

Hầu hết thời gian, việc nhận được ngoại lệ này cho thấy rằng bạn có thể đang làm việc với một đối tượng không phải là loại bạn mong đợi:

>>>

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
1

Dòng thông báo lỗi cho an 

$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
2cho bạn biết rằng loại đối tượng cụ thể, 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
6trong trường hợp này, không có thuộc tính được truy cập, 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
7trong trường hợp này. Nhìn thấy 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
2dòng thông báo lỗi có thể giúp bạn nhanh chóng xác định thuộc tính nào bạn đã cố gắng truy cập và nơi cần đến để sửa nó.

Hầu hết thời gian, việc nhận được ngoại lệ này cho thấy rằng bạn có thể đang làm việc với một đối tượng không phải là loại bạn mong đợi:

def greet_many(people): for person in people: try: greet(person) except Exception: print('hi, ' + person) 6

Trong ví dụ trên, bạn có thể mong đợi 

$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
9là kiểu 
def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
0, có một phương thức được gọi 
def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
1. Khi bạn nhận được 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
2ngoại lệ và thấy rằng nó được nâng lên khi bạn đang cố gắng gọi 
def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
1, điều đó cho bạn biết rằng có thể bạn đang không xử lý loại đối tượng mà bạn mong đợi.

Thông thường, điều này xảy ra khi bạn đang mong đợi một đối tượng được trả về từ một hàm hoặc phương thức gọi là một kiểu cụ thể và bạn kết thúc với một đối tượng có kiểu 

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
4. Trong trường hợp này, dòng thông báo lỗi sẽ đọc 
def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
5,.

Giá trị 

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
6này được nâng lên khi có sự cố xảy ra với một câu lệnh nhập . Bạn sẽ nhận được ngoại lệ này hoặc lớp con của nó 
def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
8, nếu không tìm thấy mô-đun bạn đang cố nhập hoặc nếu bạn cố gắng nhập nội dung nào đó từ một mô-đun không tồn tại trong mô-đun. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra:

>>>

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
2

Dòng thông báo lỗi cho an 

$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
2cho bạn biết rằng loại đối tượng cụ thể, 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
6trong trường hợp này, không có thuộc tính được truy cập, 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
7trong trường hợp này. Nhìn thấy 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
2dòng thông báo lỗi có thể giúp bạn nhanh chóng xác định thuộc tính nào bạn đã cố gắng truy cập và nơi cần đến để sửa nó.

$ python example.py Traceback (most recent call last): File "/path/to/example.py", line 4, in greet('Chad') File "/path/to/example.py", line 2, in greet print('Hello, ' + someon) NameError: name 'someon' is not defined 08

Hầu hết thời gian, việc nhận được ngoại lệ này cho thấy rằng bạn có thể đang làm việc với một đối tượng không phải là loại bạn mong đợi:

Trong ví dụ trên, bạn có thể mong đợi 

$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
9là kiểu 
def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
0, có một phương thức được gọi 
def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
1. Khi bạn nhận được 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
2ngoại lệ và thấy rằng nó được nâng lên khi bạn đang cố gắng gọi 
def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
1, điều đó cho bạn biết rằng có thể bạn đang không xử lý loại đối tượng mà bạn mong đợi.

Thông thường, điều này xảy ra khi bạn đang mong đợi một đối tượng được trả về từ một hàm hoặc phương thức gọi là một kiểu cụ thể và bạn kết thúc với một đối tượng có kiểu 

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
4. Trong trường hợp này, dòng thông báo lỗi sẽ đọc 
def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
5,.

>>>

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
3

Dòng thông báo lỗi cho an 

$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
2cho bạn biết rằng loại đối tượng cụ thể, 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
6trong trường hợp này, không có thuộc tính được truy cập, 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
7trong trường hợp này. Nhìn thấy 
$ 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 
    greet_many(['Chad', 'Dan', 1])
  File "greetings.py", line 12, in greet_many
    print('hi, ' + person)
TypeError: must be str, not int
2dòng thông báo lỗi có thể giúp bạn nhanh chóng xác định thuộc tính nào bạn đã cố gắng truy cập và nơi cần đến để sửa nó.

$ python example.py Traceback (most recent call last): File "/path/to/example.py", line 4, in greet('Chad') File "/path/to/example.py", line 2, in greet print('Hello, ' + someon) NameError: name 'someon' is not defined 16

Hầu hết thời gian, việc nhận được ngoại lệ này cho thấy rằng bạn có thể đang làm việc với một đối tượng không phải là loại bạn mong đợi:

Được nâng lên khi không tìm thấy khóa ánh xạ (từ điển) trong tập hợp các khóa hiện có. (Nguồn)

Đây là một ví dụ về việc 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
16được nêu ra:

>>>

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
4

Dòng thông báo lỗi cho a 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
16cung cấp cho bạn khóa không thể tìm thấy. Điều này không còn nhiều để tiếp tục nhưng, kết hợp với phần còn lại của quá trình theo dõi, thường là đủ để khắc phục sự cố.

Để có cái nhìn sâu hơn 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
16, hãy xem các Ngoại lệ KeyError trong Python và Cách xử lý Chúng .

# greetings.py def who_to_greet(person): return person if person else input('Greet who? ') def greet(someone, greeting='Hello'): print(greeting + ', ' + who_to_greet(someone)) def greet_many(people): for person in people: try: greet(person) except Exception: print('hi, ' + person) 3

Giá trị 

# greetings.py
def who_to_greet(person):
    return person if person else input('Greet who? ')

def greet(someone, greeting='Hello'):
    print(greeting + ', ' + who_to_greet(someone))

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
3này được nêu ra khi bạn tham chiếu đến một biến, mô-đun, lớp, hàm hoặc một số tên khác chưa được xác định trong mã của bạn. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra:

Được nâng lên khi không tìm thấy tên địa phương hoặc tên chung. (Nguồn)

Trong đoạn mã dưới đây, 

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8có một tham số 
# greetings.py
def who_to_greet(person):
    return person if person else input('Greet who? ')

def greet(someone, greeting='Hello'):
    print(greeting + ', ' + who_to_greet(someone))

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
8. Nhưng trong chính hàm, tham số đó đã bị sai chính tả thành 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
28:

>>>

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
5

Dòng thông báo lỗi cho a 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
16cung cấp cho bạn khóa không thể tìm thấy. Điều này không còn nhiều để tiếp tục nhưng, kết hợp với phần còn lại của quá trình theo dõi, thường là đủ để khắc phục sự cố.

Để có cái nhìn sâu hơn 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
16, hãy xem các Ngoại lệ KeyError trong Python và Cách xử lý Chúng .

>>>

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
6

Dòng thông báo lỗi cho a 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
16cung cấp cho bạn khóa không thể tìm thấy. Điều này không còn nhiều để tiếp tục nhưng, kết hợp với phần còn lại của quá trình theo dõi, thường là đủ để khắc phục sự cố.

$ python example.py Traceback (most recent call last): File "/path/to/example.py", line 4, in greet('Chad') File "/path/to/example.py", line 2, in greet print('Hello, ' + someon) NameError: name 'someon' is not defined 32

Để có cái nhìn sâu hơn 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
16, hãy xem các Ngoại lệ KeyError trong Python và Cách xử lý Chúng .

# greetings.py
def who_to_greet(person):
    return person if person else input('Greet who? ')

def greet(someone, greeting='Hello'):
    print(greeting + ', ' + who_to_greet(someone))

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
3

Giá trị 

# greetings.py
def who_to_greet(person):
    return person if person else input('Greet who? ')

def greet(someone, greeting='Hello'):
    print(greeting + ', ' + who_to_greet(someone))

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
3này được nêu ra khi bạn tham chiếu đến một biến, mô-đun, lớp, hàm hoặc một số tên khác chưa được xác định trong mã của bạn. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra:

>>>

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
7

Dòng thông báo lỗi cho a 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
16cung cấp cho bạn khóa không thể tìm thấy. Điều này không còn nhiều để tiếp tục nhưng, kết hợp với phần còn lại của quá trình theo dõi, thường là đủ để khắc phục sự cố.

Để có cái nhìn sâu hơn 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
16, hãy xem các Ngoại lệ KeyError trong Python và Cách xử lý Chúng .

# example.py from greetings import greet greet(1) 2

# greetings.py
def who_to_greet(person):
    return person if person else input('Greet who? ')

def greet(someone, greeting='Hello'):
    print(greeting + ', ' + who_to_greet(someone))

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
3

Giá trị 

# greetings.py
def who_to_greet(person):
    return person if person else input('Greet who? ')

def greet(someone, greeting='Hello'):
    print(greeting + ', ' + who_to_greet(someone))

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
3này được nêu ra khi bạn tham chiếu đến một biến, mô-đun, lớp, hàm hoặc một số tên khác chưa được xác định trong mã của bạn. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra:

Được nâng lên khi không tìm thấy tên địa phương hoặc tên chung. (Nguồn)

>>>

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
8

Trong đoạn mã dưới đây, 

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
8có một tham số 
# greetings.py
def who_to_greet(person):
    return person if person else input('Greet who? ')

def greet(someone, greeting='Hello'):
    print(greeting + ', ' + who_to_greet(someone))

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
8. Nhưng trong chính hàm, tham số đó đã bị sai chính tả thành 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
28:

Dòng thông báo lỗi của truy xuất 

# greetings.py
def who_to_greet(person):
    return person if person else input('Greet who? ')

def greet(someone, greeting='Hello'):
    print(greeting + ', ' + who_to_greet(someone))

def greet_many(people):
    for person in people:
        try:
            greet(person)
        except Exception:
            print('hi, ' + person)
3cung cấp cho bạn tên bị thiếu. Trong ví dụ trên, đó là một biến hoặc tham số sai chính tả cho hàm được truyền vào.

  • # greetings.py
    def who_to_greet(person):
        return person if person else input('Greet who? ')
    
    def greet(someone, greeting='Hello'):
        print(greeting + ', ' + who_to_greet(someone))
    
    def greet_many(people):
        for person in people:
            try:
                greet(person)
            except Exception:
                print('hi, ' + person)
    
    3cũng sẽ được nâng lên nếu đó là tham số bạn viết sai chính tả:
  • Ở đây, có vẻ như bạn không làm gì sai. Dòng cuối cùng đã được thực thi và được tham chiếu trong bản truy xuất có vẻ tốt. Nếu bạn thấy mình ở trong tình huống này, thì việc cần làm là xem qua mã của bạn để biết nơi 
    # greetings.py
    def who_to_greet(person):
        return person if person else input('Greet who? ')
    
    def greet(someone, greeting='Hello'):
        print(greeting + ', ' + who_to_greet(someone))
    
    def greet_many(people):
        for person in people:
            try:
                greet(person)
            except Exception:
                print('hi, ' + person)
    
    8biến được sử dụng và xác định. Ở đây bạn có thể nhanh chóng thấy rằng tên tham số đã bị sai chính tả.

Điều 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
32này được nâng lên khi bạn có cú pháp Python không chính xác trong mã của mình. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra:

Tăng lên khi trình phân tích cú pháp gặp lỗi cú pháp. (Nguồn)

$ python example.py Traceback (most recent call last): File "/path/to/example.py", line 4, in greet('Chad') File "/path/to/example.py", line 2, in greet print('Hello, ' + someon) NameError: name 'someon' is not defined 52

Dưới đây, vấn đề là dấu hai chấm bị thiếu ở cuối dòng định nghĩa hàm. Trong Python REPL, lỗi cú pháp này xuất hiện ngay sau khi nhấn enter:

Dòng thông báo lỗi 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
32duy nhất cho bạn biết rằng có vấn đề với cú pháp mã của bạn. Nhìn vào các dòng ở trên cho bạn dòng có vấn đề và thường là 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
35dấu mũ (dấu mũ) chỉ đến điểm có vấn đề. Ở đây, 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
36câu lệnh của hàm bị thiếu dấu hai chấm .

Ngoài ra, với tính năng theo dõi 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
32, dòng đầu tiên thông thường 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
38bị thiếu. Đó là bởi vì giá trị 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
32được nâng lên khi Python cố gắng phân tích cú pháp mã của bạn và các dòng không thực sự được thực thi.

>>>

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
9

Giá trị 

# example.py
from greetings import greet

greet(1)
2này được nâng lên khi mã của bạn cố gắng thực hiện điều gì đó với một đối tượng không thể làm điều đó, chẳng hạn như cố gắng thêm một chuỗi vào một số nguyên hoặc gọi 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
42một đối tượng mà độ dài của nó không được xác định. Tài liệu Python xác định khi nào ngoại lệ này được đưa ra:

  1. Nâng lên khi một hoạt động hoặc chức năng được áp dụng cho một đối tượng thuộc loại không phù hợp. (Nguồn)

  2. Trong ví dụ thứ hai, vấn đề là bạn đang nhận quá nhiều giá trị và không đủ biến để giải nén chúng vào.

Làm thế nào để bạn ghi lại một dấu vết?

Nhận được một ngoại lệ và truy xuất lại Python kết quả của nó có nghĩa là bạn cần quyết định phải làm gì với nó. Thông thường sửa mã của bạn là bước đầu tiên, nhưng đôi khi vấn đề là do đầu vào không mong muốn hoặc không chính xác. Mặc dù việc cung cấp cho những tình huống đó trong mã của bạn là rất tốt, nhưng đôi khi cũng có ý nghĩa nếu bạn im lặng hoặc ẩn ngoại lệ bằng cách ghi lại dấu vết và làm điều gì đó khác.

Dưới đây là một ví dụ thực tế hơn về mã cần chặn một số truy nguyên Python. Ví dụ này sử dụng 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
59thư viện . Bạn có thể tìm hiểu thêm về nó trong Thư viện yêu cầu của Python (Hướng dẫn) :

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
0

Mã này hoạt động tốt. Khi bạn chạy tập lệnh này, cung cấp cho nó một URL làm đối số dòng lệnh , nó sẽ gọi URL và sau đó in mã trạng thái HTTP và nội dung từ phản hồi. Nó thậm chí hoạt động nếu phản hồi là trạng thái lỗi HTTP:

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
1

Tuy nhiên, đôi khi URL mà tập lệnh của bạn được cung cấp để truy xuất không tồn tại hoặc máy chủ lưu trữ không hoạt động. Trong những trường hợp đó, tập lệnh này bây giờ sẽ đưa ra một 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
60ngoại lệ không cần thiết và in ra một dấu vết:

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
2

Quá trình truy xuất Python ở đây có thể rất dài với nhiều trường hợp ngoại lệ khác được đưa ra và cuối cùng dẫn đến việc 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
60được nâng lên bởi 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
59chính nó. Nếu bạn chuyển lên theo dõi ngoại lệ cuối cùng, bạn có thể thấy rằng tất cả vấn đề đều bắt đầu trong mã của chúng tôi với dòng 5 của 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
63.

Nếu bạn bọc dòng vi phạm trong một 

# greetings.py
...
greet_many(['Chad', 'Dan', 1])
4và 
# greetings.py
...
greet_many(['Chad', 'Dan', 1])
5chặn , việc bắt được ngoại lệ thích hợp sẽ cho phép tập lệnh của bạn tiếp tục hoạt động với nhiều đầu vào hơn:

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
3

Đoạn mã trên sử dụng một 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
66mệnh đề với khối 
# greetings.py
...
greet_many(['Chad', 'Dan', 1])
4và 
# greetings.py
...
greet_many(['Chad', 'Dan', 1])
5. Nếu bạn không quen với tính năng này của Python, hãy xem phần về 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
66mệnh đề trong Python Exceptions: An Introduction .

Bây giờ khi bạn chạy tập lệnh với một URL sẽ dẫn đến 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
60việc được nâng lên, bạn sẽ nhận được in 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
71mã trạng thái và nội dung 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
72:

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
4

Điều này hoạt động tuyệt vời. Tuy nhiên, trong hầu hết các hệ thống thực, bạn không muốn chỉ bỏ qua ngoại lệ và kết quả theo dõi, mà bạn muốn ghi lại quá trình theo dõi. Ghi nhật ký theo dõi cho phép bạn hiểu rõ hơn về những gì xảy ra trong chương trình của bạn.

Lưu ý: Để tìm hiểu thêm về hệ thống ghi nhật ký của Python, hãy xem Ghi nhật ký bằng Python .

Bạn có thể ghi lại dấu vết trong tập lệnh bằng cách nhập 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
73gói, nhận trình ghi và gọi trình 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
74ghi đó trong 
# greetings.py
...
greet_many(['Chad', 'Dan', 1])
5phần 
# greetings.py
...
greet_many(['Chad', 'Dan', 1])
4và 
# greetings.py
...
greet_many(['Chad', 'Dan', 1])
5khối. Tập lệnh cuối cùng của bạn sẽ giống như đoạn mã sau:

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
5

Bây giờ khi bạn chạy tập lệnh cho một URL có vấn đề, nó sẽ in ra dự kiến 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
71và 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
72nhưng nó cũng sẽ ghi lại dấu vết:

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
6

Theo mặc định, Python sẽ gửi thông báo nhật ký đến lỗi chuẩn ( 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
80). Điều này có vẻ như chúng tôi đã không triệt tiêu đầu ra theo dõi. Tuy nhiên, nếu bạn gọi lại nó trong khi chuyển hướng 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
80, bạn có thể thấy rằng hệ thống ghi nhật ký đang hoạt động và chúng tôi có thể lưu nhật ký của mình để sử dụng sau:

>>> def greet(someone):
...   print('Hello, ' + someon)
... 
>>> greet('Chad')
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in greet
NameError: name 'someon' is not defined
7

Phần kết luận

Truy nguyên Python chứa thông tin tuyệt vời có thể giúp bạn tìm ra điều gì đang xảy ra trong mã Python của bạn. Những dấu vết này có thể trông hơi đáng sợ, nhưng một khi bạn chia nhỏ nó ra để xem những gì nó đang cố gắng cho bạn thấy, chúng có thể cực kỳ hữu ích. Xem qua từng dòng một theo dõi sẽ giúp bạn hiểu rõ hơn về thông tin mà chúng chứa đựng và giúp bạn tận dụng tối đa chúng.

Nhận đầu ra theo dõi Python khi bạn chạy mã của mình là một cơ hội để cải thiện mã của bạn. Đó là một cách Python cố gắng giúp bạn.

Bây giờ bạn đã biết cách đọc một truy nguyên Python, bạn có thể hưởng lợi từ việc tìm hiểu thêm về một số công cụ và kỹ thuật để chẩn đoán các vấn đề mà đầu ra theo dõi của bạn đang cho bạn biết. 

$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
82Mô-đun tích hợp của Python có thể được sử dụng để làm việc và kiểm tra các truy nguyên. Các 
$ python example.py
Traceback (most recent call last):
  File "/path/to/example.py", line 4, in 
    greet('Chad')
  File "/path/to/example.py", line 2, in greet
    print('Hello, ' + someon)
NameError: name 'someon' is not defined
82mô-đun có thể hữu ích khi bạn cần phải nhận được nhiều hơn từ các đầu ra traceback. Cũng sẽ hữu ích nếu tìm hiểu thêm về một số kỹ thuật gỡ lỗi mã Python của bạn.

Hướng dẫn traceback python - con trăn theo dõi

Các khóa học miễn phí qua video:
Lập trình C Java C# SQL Server PHP HTML5-CSS3-JavaScript
« Prev: Python: Các kiểu dữ liệu cơ bản trong Python Prev: Python: Các kiểu dữ liệu cơ bản trong Python
» Next: Python: Bố cục PyQt: Tạo các ứng dụng GUI chuyên nghiệp Next: Python: Bố cục PyQt: Tạo các ứng dụng GUI chuyên nghiệp

Copied !!!