Cookie Policy

We use cookies to operate this website, improve usability, personalize your experience, and improve our marketing. Privacy Policy.

By clicking "Accept" or further use of this website, you agree to allow cookies.

Accept
Learn Machine Learning by Doing Learn Now
You are reading solutions / Python
alfie-grace-headshot-square2.jpg
Author: Alfie Grace
Data Scientist

SyntaxError: unexpected EOF while parsing

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

Example 1:

The most common cause of this error is usually a missing punctuation mark somewhere. Let's consider the following print statement:

print('This is a test'

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.

Solution

print('This is a test')
Out:
This is a test

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 (), brackets [], 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.

Both PyCharm (IDE) and Visual Studio Code (advanced text editor) are great options if you are looking for better syntax highlighting.

Example 2

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,

example_list = [1, 2, 3, 4, 5]

for i, item in enumerate(example_list):
    print(f'index : {i} value : {item}'
Out:
File "<ipython-input-3-babbd3ba1063>", line 4
    print(f'index : {i} value : {item}'
                                       ^
SyntaxError: unexpected EOF while parsing

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:

Solution

example_list = [1, 2, 3, 4, 5]

for i, item in enumerate(example_list):
    print(f'index : {i} value : {item}')
Out:
index : 0 value : 1
index : 1 value : 2
index : 2 value : 3
index : 3 value : 4
index : 4 value : 5

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:

example_list = [1, 2, 3, 4, 5]

for item in example_list:
Out:
File "<ipython-input-5-5927dfecd199>", line 3
    for item in example_list:
                             ^
SyntaxError: unexpected EOF while parsing

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.

Note

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:

Solution

example_list = [1, 2, 3, 4, 5]

for item in example_list:
    print(item)

In this case, going with a simple print statement allowed Python to move on after the for loop. We didn't have to go specifically with a print statement, though. Python just needed something indented to detect what code to execute while iterating through the for loop.

Cause 3: Unfinished try statement

When using try to handle exceptions, you need always to include at least one except or finally clause. It's tempting to test if something will work with try, but this is what happens:

try:
    print('hello')
Out:
File "<ipython-input-7-20a42a968335>", line 2
    print('hello')
                  ^
SyntaxError: unexpected EOF while parsing

Since Python is expecting at least one except or finally clause, you could handle this in two different ways. Both options are demonstrated below.

Solution

try:
    print('hello')
except:
    print('failed')
try:
    print('hello')
finally:
    print('printing anyway')
Out:
hello
printing anyway

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!

Summary

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.


Meet the Authors

alfie-grace-headshot-square2.jpg

Alfie graduated with a Master's degree in Mechanical Engineering from University College London. He's currently working as Data Scientist at Square Enix. Find him on LinkedIn.

Brendan Martin
Editor: Brendan
Founder of LearnDataSci

Get updates in your inbox

Join over 7,500 data science learners.