Home › Errors › SyntaxError

Python SyntaxError: the file never started running

Exception: SyntaxErrorCategory: parse timeRaised at: before any line runs

What this error means

A SyntaxError is not a bug in your logic — it is Python refusing to compile your file. The interpreter reads the whole module and turns it into bytecode before it executes a single statement, and the parser hit a sequence of tokens that no Python grammar rule allows.

That timing is the most useful thing to know about this error. Nothing ran: not the print on line 1, not the import, not the code above the reported line. If you expected some output before the crash and got none, the exception is almost certainly a SyntaxError (or its subclass IndentationError).

Since Python 3.10 the messages are far more specific than the old catch-all invalid syntax: you now get expected ':', '(' was never closed, and even Maybe you meant '==' or ':=' instead of '='?. Read the message first — in most cases it names the fix.

A real example

scores = [90, 85, 72]

if len(scores) > 0
    print(sum(scores) / len(scores))
  File "report.py", line 3
    if len(scores) > 0
                      ^
SyntaxError: expected ':'

Note what the traceback does not have: no Traceback (most recent call last) header and no call stack. There is no stack because no function ever ran. A SyntaxError report is just a file, a line, a caret and a message.

The causes behind almost every SyntaxError

Why the caret often points at the wrong line

The parser reports where it discovered the problem, not where you made it. An unclosed bracket keeps the expression open, so the first token that cannot possibly continue it — usually on the next line — gets the caret:

total = sum([10, 20, 30
print("total:", total)
  File "report.py", line 1
    total = sum([10, 20, 30
                ^
SyntaxError: '[' was never closed

The rule of thumb: when the flagged line looks perfectly fine, the real error is on the line (or lines) above it. Modern Python points back at the opening bracket for you; older versions just said invalid syntax on line 2.

How to debug it

  1. Read the message, not just the error name — expected ':' and '(' was never closed each name the fix outright.
  2. Look at the reported line and the line above it. Unclosed brackets, quotes and missing commas always report late.
  3. Let the editor help: matching-bracket highlighting and syntax colouring expose an unterminated string instantly — the rest of the file turns string-coloured.
  4. Comment out the suspect block and re-run. If the error moves, you have isolated the region; if it does not, the real problem is earlier.
  5. Check the Python version mismatch if you copied the code: print without parentheses and except Exception, e are Python 2 syntax.

Fix it interactively

Each of these free problems is a one-token syntax bug you repair in the browser — no setup, instant feedback:

Practice more Python bugs →

Related errors

Full catalogue of messages and fixes: the Python errors guide. For the search-and-narrow routine itself, read the Python debugging guide.

🇮🇳 Hindi में समझें

SyntaxError का मतलब है कि Python आपका code पढ़ ही नहीं पाया — program शुरू ही नहीं हुआ, इसलिए ऊपर का कोई print भी नहीं चला। 90% मामलों में वजह यही होती है: : छूट गया, == की जगह = लिख दिया, या कोई bracket/quote बंद नहीं हुआ। एक ज़रूरी बात — caret (^) अक्सर अगली line पर दिखता है, इसलिए हमेशा एक line ऊपर भी देखो।

पूरी Hindi explanation पढ़ें →