SoFunction
Updated on 2024-11-19

Ultra-comprehensive python common errors and solutions combing must be collected!

AttribteError: ‘module' object has no attribute xxx'

Description:The module has no associated properties. Possible Causes.

1. Naming the .py file with a Python reserved word or the same as the module name, etc.

Solution: Change the file name

The file caches code that has not been updated.

Solution: Delete the .pyc file for the library.

AttributeError: ‘Obj' object has no attribute ‘attr'

Description:The object has no associated properties. Possible Causes.

Built-in objects have no attributes.

Solution: Remove related access

No properties are set for custom objects.

Solution: Adding Properties to Custom Objects

3. Access instance attributes by class name.

Solution: Change to access by instance name

FileExistsError: [Errmo 17] File exists

Description:The file already exists.

Solution:First determine whether the file exists, if it already exists, it will not be re-created

FileNotFoundError: [Ermo 2] No such file or directory

Description:The requested file or directory does not exist.

Solution: Check if the path of the file or directory is correct

IndentationError: expected an indented block

Description:Expecting an indented block. Possible Causes.

1. Define a function or a structure under a class without indentation.

Solution: Add indentation

Or structures under for or while statements are not indented.

Solution: Add indentation

IndentationError: unexpected indent

Description:Indentation error. Possible Causes.

Code is preceded by spaces other than indentation.

Solution: Remove extra spaces

IndentationError: unindent does not match any outer indentation level

Description:Placeholder problem. Possible Causes.

1. Code blocks at the same level use different indentation rules ( code is not aligned ).

Solution: Align with keys

2. The presence of illegal characters or invisible content in other formats.

Solution: Remove illegal characters

IndexError: list index out of range

Description:List subscript out of bounds, possible causes

1. The index value specified when fetching an element exceeds the length of the list.

Solution: Modify the index value

2. The list is empty, there will be an error when getting the elements.

Solution: Determine that it is not empty and then get it.

IndexError: string index out of range

Description:String subscript out of bounds, possible causes

1. The specified index does not exist when performing string interception.

Solution: Modify the index value

2. According to the index to get a character in the string when the specified index does not exist.

Solution: Modify the index value or make a judgment call first.

sADirectoryError: [Ermo 21] Is a directory

Description:Wanted to manipulate a file but was provided with a directory error. Possible causes.

1. Operate on directories as files, e.g., test is a directory, using (test) will raise an error.

Solution: Add the corresponding file name

2. Forget to write the file extension.

Solution: Complete the file name

ITypeError: _ init _()takes 0 positional arguments but 1 was given

Description:The number of position parameters is incorrect. Possible causes.

No self argument was added to the _- init__() method.

Solution: Add the self parameter

KeyError: ‘age'

Description:Key error. Possible Causes.

1. The specified key does not exist when getting the value of the dictionary based on the key.

Solution: Change to existing key

2. Delete a non-existent key.

Solution: Determine if it exists before deleting it.

ModuleNotFoundError: No module named' pymysql '

Description:Module does not exist, possible causes.

1. The module name is misspelled.

Solution: Modify correctly

2. No module was introduced.

Solution: Use the import statement to import a module.

3. No third-party modules were downloaded.

Solution: Use pip to install the required modules

NameError:name test' is not defined

Description:A local or global variable name was not found. Possible causes.

1. Variables are not defined.

Solution: Define variables

2. Python version 3 does not support some functions or methods in Python 2, such as xrange().

Solution:Modify to a function or method in Python 3

RecursionEror:maximum rcusion depth exceded in com*

Description:Maximum recursion depth exceeded

Solution: Disable infinite recursion, set recursion depth

SyntaxError: EOL whilescanning string literal

Description:String quotes do not appear in pairs. Possible scenarios.

1. Forget to write the closing quotation marks.

Solution: Add missing quotes

2. Include \ at the end of the string.

Solution: Replace \ with \ or /.

3. Nesting error in quotation marks.

Solution:Suggested use of outer single quotes, inner double quotes

SyntaxError: invalid syntax

Description:Invalid syntax. Possible Causes.

1. Forget to write a colon after an if, while, for, etc. statement, or write other symbols such as a colon writing a semicolon.

Solution: Change to an English half-colon.

2. The parentheses of a function or method are written as <>.

Solution: Modify it to English half-point ()

3. An equal sign = appears in the conditional expression.

Solution: Change it to the comparison operator ==.

4. Define functions without parameters without parentheses.

Solution: Add parentheses

Syntax Error: non-default argument follows default argument

Description:Parameters are defined in the wrong order.

Possible: The default value parameter is not placed at the end of all parameters.

Solution: Change the order of parameters

SyntaxError : invalid character in identifier

Description:Invalid character in identifier.

Possible causes.

1. The parentheses of the function or method are typed in Chinese state.

Solution: Modify the input to English half-width state

2. String delimiters use Chinese quotes.

Solution: Modify the input to English half-width state

3. Include full-width spaces in lines of code.

Solution: Remove or modify to half-width spaces

TimeouEror [WinEror 10060The connection attempt failed because the connected party did not reply correctly after a certain period of time or the connected host did not respond.

Description:Connection timeout. Possible causes.

1. Database connection timeout.

Solution: Check if the database connection configuration information is correct

2. The request URL times out.

Solution: Check whether the URL is correct and whether it can be accessed normally.

3. Connection to the FTP service times out.

Solution: Check whether the parameters for connecting to FTP are correct or not

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

Description:Only string to string concatenation can be performed.

Possible cause: Concatenates strings with data of non-string types (e.g., integers, floats, booleans, sequence objects, etc.) using +.

Solution: Use the str() function to convert and then concatenate

TypeError: f() takes exactly 2 arguments (1 given)

Description:Not enough arguments were provided for the function.

Possible scenarios: Define two parameters and pass only one when calling.

Solution: Complete the parameter

TypeError: ‘tuple' object does not support item assignment

Description:Cannot modify tuple elements.

Possible cause: Fetching tuple elements by index and modifying them.

Solution: Modify tuple to list or delete modification operation

ValueError: could not convert string to float:‘12.2moon'

Description:Cannot convert a string to a floating point number.

Possible causes: The float() function accepts non-float string type data.

Solution: Change to floating point string

ValueError: invalid literal for int() with base 10

Description:Invalid parameter passed.

Possible causes.

() function accepts non-numeric data of type string.

Solution: Change non-numeric strings to numeric strings

2. Convert string numbers of floating-point type to integer.

Solution: Convert to floating point and then to integer.

ValueError: substring not found

Description:Substring not found.

Possible cause: The specified string does not exist when retrieving the string using the index() or rindex() method.

Solution: Judge before searching

ZeroDivisionError: division by zero

Description: 0 cannot be used as a divisor.

Possible cause: 0 is used as a divisor when performing division, rounding, or remainder operations.

Solution: Change to a non-zero number

These are the common errors reported by python and the solutions

to this article on the super-comprehensive python common errors and solutions combing must be collected on the article is introduced to this, more related python common errors content please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!