Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Python Data Development Follow

Jan 8 · 6 min read · Listen

Save

Common Python errors and their solutions

It is inevitable to encounter various errors when using Python. Here are some
common Python errors and their solutions.

1、AttribteError: ‘module’ object has no attribute xxx’


Description: The module does not have the relevant attribute. Possible causes:
1. Using a Python reserved word or the same as the module name when naming
the .py file. Solution: Modify the file name

2. The .pyc file has cached code that has not been updated. Solution: Delete the
.pyc file for that library.

2、AttributeError: ‘Obj’ object has no attribute ‘attr’


Description: The object does not have the relevant attribute. Possible causes:

1. The Python built-in object does not have the attribute. Solution: Remove the
relevant access

2. The Python custom object has not set the attribute. Solution: Add the attribute
to the custom object

3. Accessing instance attributes through the class name. Solution: Modify to access
through the instance name.

3、FileExistsError: [Errmo 17] File exists


Description: The file already exists. Solution: First check if the file exists, and if it
does, do not create it again.

FileNotFoundError: [Ermo 2] No such file or directory Description: The requested


file or directory does not exist. Solution: Check if the path to the file or directory is
correct.

4、IndentationError: expected an indented block


Description: Expected an indented block. Possible causes:

1. No indentation when defining the structure under a function or class. Solution:


Add indentation
2. No indentation under the structure of an if, for, or while statement. Solution:
Add indentation

5、IndentationError: unexpected indent


Description: Indentation error. Possible causes: Extra spaces appear in front of the
code, besides indentation. Solution: Delete the extra spaces.

6、IndentationError: unindent does not match any outer indentation


level
Description: Indentation issues. Possible causes:

1. Different indentation rules are used for code blocks at the same level (code is
not aligned). Solution: Use the key to align the code

2. There are illegal characters or other format of invisible content. Solution:


Remove the illegal characters.

7、IndexError: list index out of range


Description: List index out of range, possible causes:

1. The index value specified when getting the element exceeds the length of the
list. Solution: Modify the index value

2. The list is empty, and an error occurs when getting the element. Solution: Check
if it is not empty before getting it.

8、IndexError: string index out of range


Description: String index out of range, possible causes:
1. When performing string slicing, the specified index does not exist. Solution:
Modify the index value

2. When getting a character from a string based on an index, the specified index
does not exist. Solution: Modify the index value or first check if it exists.

9、sADirectoryError: [Ermo 21] Is a directory


Description: An error occurs when trying to operate on a file, but a directory is
provided instead. Possible causes:

1. Operating on a directory as a file, for example, if “test” is a directory, using


os.remove(test) will cause an error. Solution: Add the corresponding file name

2. Forgetting to write the file extension. Solution: Complete the file name.
ITypeError: init()takes 0 positional arguments but 1 was given Description:
Incorrect number of positional arguments. Possible causes: The self argument
was not added to the init() method. Solution: Add the self argument.

10、KeyError: ‘age’
Description: Key error. Possible causes:

1. When getting the value of a dictionary based on a key, the specified key does not
exist. Solution: Modify to an existing key

2. Deleting a non-existent key. Solution: Check if it exists before deleting it.

11、ModuleNotFoundError: No module named’ pymysql ’


Description: The module does not exist. Possible causes:

1. The module name is spelled incorrectly. Solution: Correct the spelling


2. The module is not imported. Solution: Use the import statement to import the
module

3. The third-party module is not downloaded. Solution: Use pip to install the
required module.

12、NameError:name test’ is not defined


Description: The name of a local or global variable is not found. Possible causes:

1. The variable is not defined. Solution: Define the variable

2. Some functions or methods in Python2, such as xrange(), are not supported in


Python3. Solution: Modify to a function or method in Python3.

13、RecursionEror:maximum rcusion depth exceded in comprison


Description: Exceeded the maximum recursion depth Solution: Prohibit infinite
recursion and set the recursion depth. SyntaxError: EOL while scanning string
literal Description: The quotes in the string do not appear in pairs. Possible
situations:

1. Forgetting to write the end quotes. Solution: Add the missing quotes

2. Including \ at the end of the string. Solution: Replace \ with \ or /

3. Quotation nesting error. Solution: It is recommended to use single quotes for the
outer layer and double quotes for the inner layer.

14、SyntaxError: invalid syntax


Description: Invalid syntax. Possible reasons:

1. Forgetting to write a colon after an if, while, for, etc. statement, or writing a
colon as a semicolon or other symbol. Solution: Change to an English half-width
colon.

2. Writing the parentheses of a function or method as <>. Solution: Change to


English half-width parentheses.

3. An equal sign (=) appearing in a condition expression. Solution: Change it to a


comparison operator (==).

4. Not adding parentheses when defining a function with no arguments. Solution:


Add parentheses.

15、Syntax Error: non-default argument follows default argument


Description: The order of the defined parameters is incorrect. Possible scenarios:

The default value parameters are not placed at the end of all parameters.

Solution: Change the order of the parameters.

16、SyntaxError : invalid character in identifier


Description: Invalid characters in identifier. Possible causes:

1. The parentheses of a function or method are input in Chinese mode. Solution:


Modify to input in English half-width mode

2. String delimiters use Chinese quotation marks. Solution: Modify to input in


English half-width mode

3. Full-width spaces are included in the code line. Solution: Remove or modify to a
half-width space.

17、Timeout error [WinError 10060] No response or response from host


after a period of time. Connection attempt failed.
Description: Connection timeout. Possible causes:
1. Database connection timeout. Solution: Check if the database connection
configuration information is correct

2. Request URL timeout. Solution: Check if the URL is correct and can be accessed

3. Connection to FTP server timeout. Solution: Check if the parameters for


connecting to FTP are correct.

18、TypeError: can only concatenate str (not “XXX”) to str


Description: Can only concatenate strings with other strings. Possible causes:

1. Attempting to concatenate a string with a non-string data type (such as an


integer, float, boolean, or sequence object). Solution: Use the str() function to
convert the data type before concatenating.

TypeError: f() takes exactly 2 arguments (1 given) Description: Not enough


arguments provided to the function. Possible cause: Defining a function with two
arguments, but only providing one when calling it. Solution: Supply the missing
arguments to complete the function call.

19、TypeError: ‘tuple’ object does not support item assignment


Description: Cannot modify tuple element. Possible causes:
Open in app Get unlimited access

1. Attempting to modify a tuple element through indexing. Solution: Convert the


tuple to a list or remove the modification operation.

20、ValueError: could not convert string to float:‘12.2month’


Description: Unable to convert string to float. Possible causes: float() function has
accepted a non-floating point string data type. Solution: Modify to a floating point
string.
21、ValueError: invalid literal for int() with base 10
Description: Invalid argument passed. Possible reasons for occurrence:

1. int() function received a string type data that is not a number. Solution: Modify
the non-numeric string to a numeric string.

2. Converting a floating point string number to an integer. Solution: Convert it to a


floating point type first and then convert it to an integer.

22、ValueError: substring not found


Description: Substring not found. Possible causes: Using the index() or rindex()
methods to search for a string, the specified string does not exist. Solution: Check
before searching.

23、ZeroDivisionError: division by zero


Description: 0 cannot be used as the divisor. Possible causes: 0 is used as the divisor
when performing division, integer division, or remainder operation. Solution:
Change to a non-zero number.
3

Python Python Programming Python3

You might also like