Python is famous for its simple syntax. However, when you are learning Python for the first time or when you come to Python with a strong foundation in another programming language, you can learn things that Python does not allow. If you get a SyntaxError
error when running your code, this article will help you solve this problem. Throughout the tutorial, you will see examples of syntax errors in Python learning how to solve them.
By the end of this tutorial, you can:
- Invalid syntax determination in Python
- Understand the traceback of
SyntaxError
- Correct and avoid syntax errors
Invalid Syntax in Python
When you run the code, the compiler first parse it to convert it into Python byte code – the code is then run. The compiler looks for syntax errors in the first step of processing the program, or the parsing stage. If the compiler cannot parse the code successfully, it means you have used the wrong syntax somewhere in your code. The compiler will try to show you where the error occurs.
When you first access Python, you may get frustrated when you get SyntaxError
errors. Python will help you determine where you use the wrong syntax but the traceback it gives may be a little confusing. Sometimes, the code it points to makes perfect sense.
Note : If the code is syntactically correct, you may receive other exceptions raised that are not
SyntaxError
. To learn more about other exceptions and how to handle them, see here
You cannot handle syntax errors like other exceptions. Even if you try to put the wrong syntax in a try...catch...
block, you will still raise the compiler to SyntaxError
.
SyntaxError
Exception and Traceback
When the compiler encounters a syntax error, Python raises the exception to SybtaxErrror
and provides a trace back with some useful information to help you debug. This is the code containing syntax errors:
1 2 3 4 5 6 7 8 9 10 11 12 | ages = { 'pam': 24, 'jim': 24 'michael': 43 } print(f'Michael is {ages["michael"]} years old.') |
You can see the invalid syntax is in dict line 4. The second item, 'jim'
, is missing commas. If you run the code, you will get the following traceback:
1 2 3 4 5 6 | $ python theofficefacts <span class="token punctuation">.</span> py File <span class="token string">"theofficefacts.py"</span> <span class="token punctuation">,</span> line <span class="token number">5</span> <span class="token string">'michael'</span> <span class="token punctuation">:</span> <span class="token number">43</span> <span class="token operator">^</span> SyntaxError <span class="token punctuation">:</span> invalid syntax |
Note that the traceback will indicate an error in line 5, not 4. The Python compiler has tried to show the error location. However, it can only point to where it caught the first error. When you get a traceback and a traceback looks meaningful, you need to look at the type of code to determine where it is wrong.
In the example above, there’s no problem with missing commas but depends on what follows. For example, the absence of a comma after 'michael'
in line 5 has no effect. But once the compiler encounters something wrong, it can only point to the first line of code that it doesn’t understand.
Note: This tutorial assumes that you already have a basic knowledge about traceback in Python. To learn more about how the traceback and reading them, reading this .
There are several components of SyntaxError
that can help you locate errors:
- File name – The file where the syntax error was found
- Line number – error line content
- Caret – the exact position where the error was encountered
- Error message – immediately after the
SybtaxError
type of error, the error content helps you identify the problem
In the example above, the file name is theofficefacts.py
, the line number is 5
and the caret points to the single quotes of the michael
key. The SyntaxError
Traceback may not indicate a real problem but it will point to the first position that the compiler does not understand.
There are two other exceptions you will likely see Python raise. It is similar to SyntaxError
but under other names:
IndentationError
TabError
These exceptions all inherit from SyntaxError
but they are special cases related to indentation. IndentationError
is raised when indent levels do not match. And TabError
appears when your code uses both tabs and space to ring the same file. We will take a closer look at these exceptions later.
Common Syntax Problems
When we first encountered SyntaxError
, it was helpful to know the reason for the error and how to fix it. In the following sections, we will see some common reasons that SyntaxError
can be raised and how you can fix it.