SyntaxError: unexpected EOF while parsing
Why does this occur?
EOF stands for "end of file," and this syntax error occurs when Python detects an unfinished statement or block of code. This can happen for many reasons, but the most likely cause is missing punctuation or an incorrectly indented block.
In this lesson, we'll examine why the error
SyntaxError: unexpected EOF while parsing can occur. We'll also look at some practical examples of situations that could trigger the error, followed by resolving it.
Cause 1: Missing or Unmatched Parentheses
The most common cause of this error is usually a missing punctuation mark somewhere. Let's consider the following
As you may have noticed, the statement is missing a closing parenthesis on the right-hand side. Usually, the error will indicate where it experienced an unexpected end-of-file, and so all we need to do here is add a closing parenthesis where the caret points.
In this case, adding in the closing parenthesis has shown Python where print statement ends, which allows the script to run successfully.
This occurrence is a reasonably simple example, but you will come across more complex cases, with some lines of code requiring multiple matchings of parentheses
, and braces
To avoid this happening, you should keep your code clean and concise, reducing the number of operations occurring on one line where suitable. Many IDEs and advanced text editors also come with built-in features that will highlight different pairings, so these kinds of errors are much less frequent.
Building on the previous example, let's look at a more complex occurrence of the issue.
It's common to have many sets of parentheses, braces, and brackets when formatting strings. In this example, we're using an f-string to insert variables into a string for printing,
Like the first example, a missing parenthesis causes the error at the end of the print statement, so Python doesn't know where the statement finishes. This problem is corrected as shown in the script below:
In this situation, the solution was the same as the first example. Hopefully, this scenario helps you better visualize how it can be harder to spot missing punctuation marks, throwing an error in more complex statements.
Cause 2: Empty Suite
The following code results in an error because Python can't find an indented block of code to pair with our
for loop. As there isn't any indented code, Python doesn't know where to end the statement, so the interpreter gives the syntax error:
A statement like this without any code in it is known as an empty suite. Getting the error, in this case, seems to be most common for beginners that are using an ipython console.
As of Python 3.9, running the same code throws the error IndentationError: expected an indented block instead.
We can remedy the error by simply adding an indented code block:
In this case, going with a simple
for loop. We didn't have to go specifically with a
Cause 3: Unfinished try statement
try to handle exceptions, you need always to include at least one
finally clause. It's tempting to test if something will work with
try, but this is what happens:
Since Python is expecting at least one
finally clause, you could handle this in two different ways. Both options are demonstrated below.
In the first option, we're just making a simple print statement for when an exception occurs. In the second option, the
finally clause will always run, even if an exception occurs. Either way, we've escaped the SyntaxError!
This error gets triggered when Python can't detect where the end of statement or block of code is. As discussed in the examples, we can usually resolve this by adding a missing punctuation mark or using the correct indentation. We can also avoid this problem by keeping code neat and readable, making it easier to find and fix the problem whenever the error does occur.