← Back

Home › Errors › SyntaxError

SyntaxError: unexpected EOF while parsing

Exception: SyntaxErrorCategory: compile time (parsing)Raised at: the end of the file or input, once the parser notices an open construct

What this error means

EOF means end of file. The parser was still in the middle of something — an open bracket, an unfinished string, a block waiting for its body — and the input simply ran out.

The important consequence: none of your code ran. This is a compile-time error. Python reads the whole file into bytecode before executing a single line, so a syntax error anywhere stops everything, including the parts that look correct.

Traceback (most recent call last):
  File "demo.py", line 9
    print(items
              ^
SyntaxError: unexpected EOF while parsing

Read the caret. It points at the last character the parser managed to read before running out of input — which is why it usually sits at the end of a line rather than at the actual mistake.

Python 3.10 changed this message

If you are on Python 3.10 or newer you will rarely see unexpected EOF while parsing. The parser was rewritten and now names the specific construct it is still waiting for:

Python 3.9 and earlier:  SyntaxError: unexpected EOF while parsing
Python 3.10 and later:   SyntaxError: '(' was never closed
                         SyntaxError: expected ':'
                         SyntaxError: ''' was never closed
                         SyntaxError: invalid syntax

The older wording is still one of the most-searched Python errors because a huge amount of documentation, tutorials, coursework and Stack Overflow answers were written against Python 3.6–3.9. Seeing it usually means you are on an older interpreter, or reading older material.

The five unclosed constructs

# 1 — unclosed bracket, paren or brace
print(items            # ❌ missing )
total = sum([1, 2, 3)  # ❌ mismatched ] vs )
data = {"a": 1,        # ❌ never closed

# 2 — unterminated string
name = "Asha           # ❌ missing closing quote

# 3 — unterminated triple-quoted string
sql = """
SELECT * FROM t        # ❌ no closing """

# 4 — a block with no body
if ready:
                       # ❌ nothing indented under it
def main():

# 5 — a trailing comma in a call on older versions
print(a, b,)           # ❌ on Python 2, fine on Python 3

Cause 4 is easy to miss because there is no visible mistake. Python needs at least one statement inside a block; if the next line is dedented or the file ends, the block is empty and the parser reports it at EOF. A pass is the explicit way to say *intentionally empty*:

def main():
    pass               # ✅ an intentionally empty body

class Config:
    ...                # ✅ same idea, the ellipsis literal

How to find it fast

  1. Look at the line number in the error, then look above it. The caret points at the end; the open construct is usually a line or two earlier.
  2. Check bracket balance on that line. Every ( needs a ), every [ a ], every { a } — and they must close in the reverse order they opened.
  3. Count your quotes. An odd number of " or ' on a line means one of them opened a string that never closed. Triple quotes are the usual culprit because they span lines.
  4. If you just pasted code, look for a line that got cut off. Copying from a web page or PDF often drops the final characters of a long line.
  5. Run python -c "import ast,sys; ast.parse(open(sys.argv[1]).read())" yourfile.py to get the parse error without executing anything else.

Most editors highlight the matching bracket when your cursor sits on one, and will warn about unbalanced quotes. If you hit this error often, turning on bracket-pair highlighting pays for itself immediately.

Interactive mode and exec

In the REPL this error has an extra cause: an incomplete statement submitted before it was finished. The interpreter normally shows ... and waits, but pasting a multi-line block, or sending input through exec() and eval(), can submit it early:

>>> def f():
...     pass            # ✅ the ... prompt means "keep going"

>>> exec("def f():")    # 💥 SyntaxError: unexpected EOF while parsing
>>> exec("def f():\n    pass")   # ✅ body included

>>> eval("[")           # 💥 same cause — an open bracket at EOF

When you build Python code as a string — for a code runner, a teaching tool, or generated source — always include the newline and the indented body. A template that produces def f(): with nothing after it is a guaranteed EOF error at runtime, in a place that looks like it has nothing to do with syntax.

Related messages worth knowing

🇮🇳 Hindi में समझें

SyntaxError: unexpected EOF while parsing me EOF ka matlab hai end of file. Parser kisi cheez ke beech me tha — ek khula bracket, ek adhoori string, ya ek block jiska body abhi aaya nahi tha — aur input khatm ho gaya. Sabse zaroori baat: iska matlab hai aapka koi bhi code chala hi nahi. SyntaxError compile-time error hai — Python pehle poore file ko bytecode me badalta hai, tab koi line execute hoti hai. Caret ^ us aakhri character ko point karta hai jo parser padh paaya, isliye wo asli galti par nahi, line ke end par hota hai. Asli problem aksar ek-do line upar hoti hai. Paanch common wajah: khula (, [ ya { jo band nahi hua; quote band na karna; triple-quoted string jo close nahi hui; ek block (if, def, for) jiske neeche koi statement hi nahi — iska fix pass likhna hai; aur purane Python versions me trailing comma. Python 3.10+ me ye message badal gaya hai — ab '(' was never closed ya expected ':' dikhta hai, jo seedha bata deta hai kya missing hai. Purani wording abhi bhi bahut search hoti hai kyunki zyada tar tutorials Python 3.6–3.9 par likhe gaye hain.

SyntaxError Hindi में पढ़ें →