python – SyntaxError: unexpected EOF while parsing

python – SyntaxError: unexpected EOF while parsing

The SyntaxError: unexpected EOF while parsing means that the end of your source code was reached before all code blocks were completed. A code block starts with a statement like for i in range(100): and requires at least one line afterwards that contains code that should be in it.

It seems like you were executing your program line by line in the ipython console. This works for single statements like a = 3 but not for code blocks like for loops. See the following example:

In [1]: for i in range(100):
  File "<ipython-input-1-ece1e5c2587f>", line 1
    for i in range(100):
                        ^
SyntaxError: unexpected EOF while parsing

To avoid this error, you have to enter the whole code block as a single input:

In [2]: for i in range(5):
   ...:     print(i, end=', ')
0, 1, 2, 3, 4,

This can simply also mean you are missing or have too many parentheses. For example this has too many, and will result in unexpected EOF:

print(9, not (a==7 and b==6)

python – SyntaxError: unexpected EOF while parsing

My syntax error was semi-hidden in an f-string

 print(f'num_flex_rows = {self.}nFlex Rows = {flex_rows}nMax elements = {max_elements}')

should be

 print(f'num_flex_rows = {self.num_rows}nFlex Rows = {flex_rows}nMax elements = {max_elements}')

It didn’t have the PyCharm spell-check-red line under the error.

It did give me a clue, yet when I searched on this error message, it of course did not find the error in that bit of code above.

Had I looked more closely at the error message, I would have found the ” in the error. Seeing Line 1 was discouraging and thus wasn’t paying close attention 🙁 Searching for

self.)

yielded nothing. Searching for

self.

yielded practically everything :-

If I can help you avoid even a minute longer of deskchecking your code, then mission accomplished 🙂

C:PythonAnaconda3python.exe
C:/Python/PycharmProjects/FlexForms/FlexForm.py File “”,
line 1
(self.)

^ SyntaxError: unexpected EOF while parsing

Process finished with exit code 1

Leave a Reply

Your email address will not be published.