Reputation: 6094
Since there are a bunch of questions here on Stack Overflow that deal with SyntaxError
in Python, we might want to know:
How do we tackle a SyntaxError
? Are there strategies that can generally be applied?
Upvotes: -1
Views: 312
Reputation: 6094
Even before running into a SyntaxError
, there are important measurements to deal with SyntaxErrors
, because the best way to deal with SyntaxErrors
is to avoid them in the first place. This can be done first and foremost by using an editor or an Integrated Development Environment (IDE) which has syntax highlighting for Python.
Besides that, we can decrease the risk of running into a SyntaxError
by good code and formatting style. There is a formal definition of the term "good formatting style", PEP 8 -- Style Guide for Python Code. Proper formatting makes our code much more readable which decreases the risk writing code that leads to a SyntaxError
.
A very good way to apply good formatting to our code is to use an automatic code formatting tool. A code formatter has multiple advantages, amongst which are the following: Its code formatting is consistent. It applies best practices you might not even have thought of yet. It is very convenient.
For Python, black is a great code formatting tool.
The Syntax Error indicates in which file and in which line the interpreter came across a problem in our code. We should use this information to find the bug.
We should be aware that the Python interpreter sometimes indicates a SyntaxError
in the line after the actual problem. This is because the parser expects something in the erroneous line and can recognise that this is missing only when the whole line has been parsed. The prototypic example for that kind of SyntaxError
is a missing parenthesis. So for instance, the following code raises a SyntaxError
in line 2
, even though the bug is in line 1
:
bar = foo(
baz()
SyntaxError: EOL while scanning string literal
. This is usually raised when you did not properly close a string definition with closing quotation marks, such as in the following example:foo = "bar
Generally, a good strategy of bug fixing is to reduce any code that throws an Error or an Exception (or that does not return the expected output) to a minimal example. (This is a requirement for questions here on Stack Overflow, but much more than this, it is a good technique for pinning down a bug.)
In case of a SyntaxError
, producing a minimal example is usually very easy, because a SyntaxError
does not depend on any values of a variable, or any state of an object or any other semantics of your code. That's why the source of a SyntaxError
is usually one line of code.
So, to identify the bug, we remove all the code besides the line that we think is the source of the Error. If the Error vanishes, it has been in a different line. If the Error persists, we try to simplify this line. For instance, we replace nested parentheses, by defining intermediate variables that hold the values:
Instead of
bar = foo(foo(baz(foo()))
the following (logically equivalent) code:
first = foo()
second = baz(first)
third = foo(second)
bar = foo(third
makes it much easier for us to identify the missing closing parenthesis.
Upvotes: 1
Reputation: 7204
Refer to documentation. Syntax Errors unfortunately cannot be captured in a Try: Except: block, so the only way to deal with them is to read the message returned, and if that doesn't help, following up with the python documentation:
Upvotes: 0