30 Most Common Errors in Python and Their Solutions

Dolyetyus

Co Admin
21 Nis 2020
1,207
667
Delft
Welcome Dear Turk Hack Team Members,

Today, I will tell you about common errors in the Python programming language and their simple solutions. Sometimes we get errors when trying to run our code in an unexpected, sometimes very frustrating way. While some of us can simply solve these troubles, some of us can make things worse while trying to solve the problem.

debugging6.png


In this topic, I will write and explain the python errors one by one and explain the possible solutions as well. So let's start.

My suggestion 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.

rNHf4y.png


Error Name: SyntaxError

Explanation: 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 a lot of ways. There may be quotes, brackets, and commas that are forgotten in the encoding. Pyhton usually writes the solution to this error to you. If you get this error, check your code more carefully. Correct any places that do not match syntax.

rNHf4y.png


Error Name: IndexError

Explanation: 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 values ​​with index in the list. If the index or value is specified incorrectly, or is not specified, edit the values.

rNHf4y.png


Error Name: ModuleNotFoundError

Explanation: Libraries are mostly used 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 the 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.

rNHf4y.png


Error Name: KeyError

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

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

rNHf4y.png


Error Name: ImportError

Explanation: Occurs when trying to pull a particular 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.

rNHf4y.png


Error Name: StopIteration

Explanation: 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 réfresh. Check these parts.

rNHf4y.png


Error Name: TypeError

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 the solution, you need to convert variables to the same type of data. For example, if you are going to multiply a string with an integer, enclose it in int ().

rNHf4y.png


Error Name: ValueError

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

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

rNHf4y.png


Error Name: NameError

Explanation: 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: Be sure of the name you assign or are trying 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.

rNHf4y.png


Error Name: ZeroDivisionError

Explanation: 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. Therefore, python also gives this error so that the program does not crash.

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

rNHf4y.png


Error Name: KeyboardInterrupt

Explanation: It may be wrong to actually count this as a complete error. Specifically, we use ctrl+c or ctrl+z to stop code running 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 avoıd this error, do not press crtl + c keys simultaneously 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.

rNHf4y.png


Error Name: AssertionError

Explanation: If the value False occurs in Assert cases, 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.

rNHf4y.png


Error Name: AttributeError

Explanation: AttributeError in Python occurs when an illegal 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.

rNHf4y.png


Error Name: EOFError

Explanation: 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.

rNHf4y.png


Error Name: FloatingPointError

Explanation: 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 avoıd this mistake is not to get too involved with the float data type. You can include try-except depending on the situation and integer the variable in case of an error. Apart from this, also pay attention to your transactions.

rNHf4y.png


Error Name: GeneratorExit

Explanation: It may not be correct to call this a complete error. If we use 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 a 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.

rNHf4y.png


Error Name: MemoryError

Explanation: This error can be received frequently by friends who are not "in good condition", especially the system they are working on. 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, since the systems are more advanced than the RAM issue, the number of encounters with this error has decreased considerably.

Solution: Solutions from easy to difficult; Close unnecessary applications running in the background and run the code, get up from the computer, go to the nearest technology store, get RAM and then plug it into the motherboard, thereby increasing your RAM memory or setting / rewriting your code to consume less system memory in general.

rNHf4y.png


Error Name: NotImplementedError

Explanation: To be honest, this error is difficult to explain simply. But I think those who received this error have enough knowledge of python 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.

rNHf4y.png


Error Name: OSError

Explanation: Operation System Error, that is, 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 the operating system for which the program was written. 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.

rNHf4y.png


Error Name: OverflowError

Explanation: This error, which we usually encounter in mathematical operations, occurs when the result of an arithmetic operation is too large to be shown. You will most likely get this error if you multiply the billions. 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.

rNHf4y.png


Error Name: socket.error errno48

Explanation: If you are using the socket library and you provide 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 you have previously connected with close() as a result of the operation.

rNHf4y.png


Error Name: RuntimeError

Explanation: Well, if you got 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 because 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 for hours.

rNHf4y.png


Error Name: IndentationError

Explanation: In programming in Python, we use spaced indents instead of using { } statements as in other languages. In general, indents in python remain default as 4 characters. 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.

rNHf4y.png


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.

rNHf4y.png


Error Name: SystemError

Explanation: They are malfunctions caused by your system. It is similar to the OSError error but this error is caused directly 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, and check the task manager and regedit paths. Since it may occur for many different reasons, it is not possible to suggest a pin point solution.

rNHf4y.png


Error Name: SystemExit

Explanation: Most likely, most people haven't even seen this error fully. 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 function I mentioned above to your code. Apart from that, you can also enclose if-else blocks if you want it to be closed in certain situations.

rNHf4y.png


Error Name: UnboundLocalError

Explanation: This problem 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 inside 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.

rNHf4y.png


Error Name: UnicodeError

Explanation: This error mainly occurs when an encoding or decoding error occurs with Unicode. The Python3 version is more fortunate 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.

rNHf4y.png


Error Name: UnicodeEncodeError

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

Solution: In the solution part, what we need to do is add the code needed 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.

rNHf4y.png


Error Name: UnicodeDecodeError

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

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

rNHf4y.png


Error Name: FileNotFoundError

Explanation: This error occurs if the file you are working on does not exist in the path you specified or does not exist directly.

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

rNHf4y.png


If you have something to add or have questions, you can write an answer to the topic. I wish you all good coding :).

 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.