Jump to content
MMOdev Moderator Applications ×

Python error guide (errors & solutions)


CallMeDaddy

Recommended Posts

My suggestion to you is that when you get an error, open this topic and open the search section with the shortcut ctrl + f , then search for the error name in the subject. This way, you can solve your problem faster.


Error Name : SyntaxError Description: It is one of the most common errors in Python programs. Occurs when a particular statement is not suitable for the intended use. Python usually reports the error along with the cause. You will encounter it as a result of using the wrong syntax. Solution: It can be solved in many ways. There may be quotes, brackets, and commas that are forgotten in the encoding. Phyton usually writes the solution for this error to you. If you get this error, check your code more carefully. Correct any places that do not match syntax.






Error Name : IndexError

Description
: IndexError is one of the more basic and common errors found in Python. We encounter this type of error when working with lists. The error occurs when trying to access a directory outside the boundaries of a list.

Solution: Check the values with index in the list. If the index or value is specified incorrectly, or not specified, edit the values.


Error Name : ModuleNotFoundError

Description
: Work mostly with libraries on Python. If the name of the library we import is wrong or the library is not installed on the system, we will get this error.

Solution: Checking the name of our library and using from import blocks may be a solution. There is also a possibility that the library is not installed. For this you need to load the library. Also, the library usage may differ depending on your python version. Some libraries no longer support python 2.




Error Name : KeyError

Description
: KeyError error in Python is an error that occurs when you try to access a key that is not in the dictionary. It is caused by not properly setting the Index and Keys.

Solution: Check the key values in your dictionary. If the key is incorrectly specified, or not specified, make the necessary adjustments.


Error Name : ImportError

Description
: Occurs when trying to pull a specific module from a library. If the module we specified in that library does not exist, we will get this error. If we get this error in the from x import y operation, it means that the y feature is not available in the x library.

Solution: If the library has a user manual, check if there is the module you want from there. Check the name you typed. Make sure you get the right module from the right library.





Error Name : StopIteration

Description
: It is an error that we may encounter while using functions. StopIteration tells us that an iterator has reached the end. When you iterate using a for loop, it is caught as an error and used to terminate the loop.

Solution: If you've used stop iteration twice, you may have gotten this error. Likewise, there may be something overlooked in the code during the refresh. Check these parts.








Explanation : We get this error when trying to use different types of variables. For example, you cannot extract an integer with a str.

Solution: For solution, you need to convert variables to same type of data. For example, if you are going to multiply a string with an integer, enclose it in int ().





Explanation : This error occurs when we assign an incorrect value to the variable we have assigned a value to. We cannot assign non-integer data to integer variables. Likewise, a boolean cannot transform into a float. Python states that there is an error in the data type and assigned value through this error.

Solution: Check the values you assigned based on the data type of a variable. You cannot integer a bool value true. Similarly, an abc string cannot be an integer. Edit the values that the variables get.



Error Name : NameError

Description
: When you encounter this error, know that there is no data in the name you specified. Likewise, a value may not be assigned. Python cannot find a variable with this name.

Solution: Make sure the name you assigned or tried to call. If the name is correct, check the data you have assigned into it. Likewise, if it is a variable inside the function and you are using it outside of the function, give a global value.


Error Name : ZeroDivisionError

Description
: As you can understand from the name, this error is received in cases of division by zero. In mathematics, you cannot divide a number by zero, it will have an undefined value. More precisely, it has no value, it cannot be known. That's why python also gives this error so that the program doesn't crash.

Solution: Don't do it if you're trying to divide any value by zero. You cannot divide by zero. Uninstall or replace that process without much tampering.



Error Name : KeyboardInterrupt

Description
: It may be wrong to actually count this as a complete error. We use ctrl + c or ctrl + z to stop code running specifically on Linux. Ctrl + z stops the program but ctrl + c closes the program. If we use these key combinations while running Python programs, we will get this error.

Solution: Actually, there is no direct solution to this. To avoid this error, do not press ctrl + c keys at the same time while the python program is open. In case of need; If you have entered an infinite loop or the program has lost its function, you can exit with these combinations.


Error Name : AssertionError

Description
: If False value occurs in Assert states, this error will be received. Assert state is a programming concept used when writing code where the user reports a condition is true using the assert statement before running the module. If Condition is True, control moves to the next line of code only. If it's false, the program stops working and gives an AssertionError Exception error.

Solution: This situation is actually up to you as before. In the simplest way, you can take advantage of this error if you want the application to close itself in case of incorrect entry in a user login application. However, if you set the other condition to not return false, you will not encounter this error.



Error Name : AttributeError

Description
: In Python, AttributeError occurs when an invalid attribute attempt is made or an attribute assignment fails. For example, if you use unsupported parameters in a data, you will get this error. For example, if you try to sum an integer value using append, you will encounter this error.

Solution: Work on each data and variable with its own attributes. For example, in the example above, if you add directly to integer instead of append, you will not have a problem. Use the attributes of the variables correctly.



Error Name : EOFError

Description
: This error occurs when the input or raw_input functions do not get a value from the user and then we call the variable assigned to this input.

Solution: One of the best ways to solve this problem is to use try-except blocks, which we can refer to in every error. Apart from that, you can force the user to enter data into the input.



Error Name : FloatingPointError

Description
: This error occurs when using libraries such as fpectl, numpy. As the name implies, it is caused by points of float-type data. If the float calculation is wrong or causes problems, we will get this error. It is possible to encounter this error in some cases if we perform operations such as 10/3.

Solution: The easiest way to avoid this error is not to get too involved with the float data type. You can include try-except according to the situation and integer the variable in case of an error. Apart from this, pay attention to the transactions you make.


Error Name : GeneratorExit

Description
: It may not be correct to call this exactly error. If we use the close function in a generator, we will get this error. We usually encounter this error when working in a loop.

Solution: If we are going to use the close function in a generator, we can do it outside of the loop. Likewise, we can use try-except here and terminate the loop with break.


Error Name : MemoryError

Explanation
: This error can be received frequently by friends who are not "in good condition" whose system is not very good. When we run codes in Python, these codes use some RAM to process. This error occurs if the RAM capacity required for python codes to run is not available. Of course, nowadays systems are more advanced than RAM, so the number of encounters with this error has decreased considerably.

SolutionSolutions from easy to difficult; Closing unnecessary applications running in the background and running the code, leaving the computer and the closest 

This is the hidden content, please
 Go to the market, buy RAM and then plug it into the motherboard, thereby increasing your RAM memory or tuning / rewriting your code to consume less system memory overall.


Error Name : NotImplementedError

Description
: To be honest, this error is hard to describe in a simple way. But I think those who received this error have enough python knowledge to understand my explanation: User-defined base classes may throw the NotImplementedError error to indicate that a method or behavior should be defined by a subclass by simulating an interface. This error is a sub-state of the RuntimeError error. In user-defined base classes, abstract methods terminate the program with this error when they need derived classes to override the method.

Solution: The simplest and smartest thing you can do to fix this error may be to direct the user by using the try-except blocks.


Error Name : OSError

Description
: Operation System Error, ie the operating system error means a problem with your operating system outside of your instant code. For example, if you encounter this error while working on Linux with a code written for Windows, there is a problem with the operating system of your computer, not your code.

Solution: Check which operating system the program was written for. If the codes written do not support your operating system, you must either change your operating system or adjust the codes according to the instant operating system.


Error Name : OverflowError

Description
: This error, which we usually encounter with mathematical operations, occurs when the result of an arithmetic operation is too large to be displayed. If you multiply the billions with each other, you will most likely get this error. An example of this issue is the 32-bit and 64-bit difference.

Solution: You can try not to work with very large files or to do very large transactions. Apart from that, you can inform the user by using try-except in such cases.


Error Name : socket.error errno48

Description
: If you are using the socket library and you establish the connection via a port that is already in use, it will give this error. The error description already means that this address is already in use. Likewise, if you do not close a port you have used before, you will get this error the next time you try to connect.

Solution: Do not use well-known ports or ports that are already used by systems. Before setting the port address, do a port search. Likewise, make sure to close the ports of your connection with close () as a result of the operation.



Error Name : RuntimeError

Description
: Good, if you get this error, take it easy. If an error does not fall into any other error category, it will appear under this name. There may be a million reasons why you encountered the error.

Solution: As I mentioned above, these errors, which do not have a certain feature, may have a million solutions, as they have million reasons. If you want to solve the problem, the most logical thing to do is to use try-except. Of course, if you say I will solve it, you have the possibility to blow your mind to solve it for hours.


Error Name : IndentationError

Description
: In Python programming, we use spaced indents instead of using {} expressions like in other languages. In general, indents in python remain 4 characters by default. However, sometimes this indent block can be 8 characters if the user wishes. This error occurs when there is an incorrect indentation.

Solution: You may have set any indentation differently, except for 4 blocks. Count the indents and correct by the set characters. Apart from this, in some cases, arrange the indents with different default values in each IDE according to the current IDE used.


Error Name : TabError

Explanation
: We can set our indents with either the TAB key or the space bar. However, we encounter this error as a result of using these keys with inconsistent and incorrect order. Likewise, after some special code blocks, there should be an indentation, but if this indentation is not, we will encounter problems.

Solution: If we go to Format> Untabify Region in Python's original IDE and press OK, we can fix the indentation settings for all code with a single click. Other than that, if you like to torture yourself, you can manually adjust the indents for each row, one by one.


Error Name : SystemError

Description
: Faults caused directly by your system. It is similar to the OSError error but this error is directly caused by your system rather than the operating system. For example, you will read and print a file, but you got this error, which means that this operation cannot be performed due to an error in the system at that moment.

Solution: The most practical ways to solve this error are to turn your computer off and on, check file operations, check system operation, task manager and regedit paths. Since it may occur for many different reasons, it is not possible to suggest a pin point solution.



Error Name : SystemExit

Description
: Most likely, most people haven't even seen this error exactly. This error is one of the exceptions like some errors, they are not exactly a problem. If you call the sys.exit () function, this function shuts down your computer and you may encounter this error before shutting down.

Solution: This error, which occurs at the user's request, does not need much solution. However, if you still do not want your computer to shut down every other two, do not add the above function to your code. Apart from that, you can also enclose if-else blocks if you want it to be closed in certain situations.


Error Name : UnboundLocalError

Description
: This issue is likely to occur in many different situations. But in summary, UnboundLocalError occurs when a local variable is called before a value is entered or fully set.

Solution: If you are working in a function, define the variable with global, assign the value first, then call the variable, create a try-except block, use the nonlocal keyword in some functions, use an editable object or pass parameters to a function. There are many ways to troubleshoot like this.



Error Name : UnicodeError

Description
: This error mainly occurs when an encoding or decoding error occurs with Unicode. The Python3 version has better luck than python2 in this regard. The encoding and decode operations required in Python 3 version are mostly done automatically.

Solution: The easiest way to solve this problem is to encode and decode respectively (usually with utf-8). You can add the code "# - * - coding: UTF-8 - * -" on the first line. Different conversion processes are required depending on the situation, python itself usually specifies these to you.


Error Name : UnicodeEncodeError

Description
: Almost identical to the error in UnicodeError, but more specifically. It tells us that there is a problem in the encode process.

SolutionWhat we need to do in the solution part is to add the required code to encode the right place. If you examine the code and enter the correct encode codes in the right place, the problem is solved. However, it will probably give UnicodeDecodeError this time.


Error Name : UnicodeDecodeError

Description
: It is the opposite of the error in UnicodeEncodeError. It tells us that there is a problem in decode process.

Solution:Whatwe need to do in the solution part is to add the code to decode the correct place. If you examine the code and enter the correct decode codes in the right place, the problem will be solved. This time, depending on the situation, you may get UnicodeTranslateError error. However, it is not common.


Error Name : FileNotFoundError

Description
: This error occurs if the file you are trying to work on does not exist in the path you specified or does not exist from the post.

Solution: You can create the file yourself, print it yourself, or search by name from the internet. Check the name of the external file you typed in your code, it may contain the wrong character.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Terms of Service