IPython raises SyntaxError: unexpected EOF while parsing (or SyntaxError: incomplete input in Python 3.10+) when its input transformer (InputTransformerManager / codeop.compile_command) misclassifies an incomplete multi-line block as syntactically finished and passes the truncated snippet straight to Python’s AST compiler—most commonly because of a trailing space or blank line terminating block continuation early, an unclosed triple-quoted string or bracket mismatch, or auto-indentation resetting to column 0 after pasting code.
Basic Example
# 1. What triggers it in an IPython REPL session:
# Typing a compound statement and pressing Enter on an unindented or blank line early:
In [1]: def greet(name):
...:
Cell In[1], line 1
def greet(name):
^
SyntaxError: incomplete input # (Shown as "SyntaxError: unexpected EOF while parsing" on Python <= 3.9)
# 2. How IPython's internal compiler (`codeop.compile_command`) distinguishes incomplete vs. broken code:
import codeop
print(codeop.compile_command("def greet(name):\n print(name)\n")) # None -> IPython waits for more lines
print(codeop.compile_command("def greet(name):\n")) # Raises SyntaxError -> IPython executes immediately!
Key Details & Edge Cases
- Why Python <= 3.9 vs. Python 3.10+ shows a different message for the exact same cause:
- Before Python 3.10’s PEG parser improvements,
compile()reported any premature end-of-file inside an unclosed block, parenthesis, or string asSyntaxError: unexpected EOF while parsing. Python 3.10+ split that into clearer errors likeSyntaxError: incomplete input,'(' was never closed, orexpected an indented block, though IPython’scheck_complete()loop uses the exact same mechanism under the hood.
- Before Python 3.10’s PEG parser improvements,
- How IPython decides whether to wait (
...:continuation prompt) or execute:- Every time you press
Enter, IPython runsTransformerManager.check_complete(cell_source)(backed bycodeop.compile_command).- If
compile_commandreturnsNone, the block is genuinely incomplete and IPython prints the...:continuation prompt. - If
compile_commandreturns a code object or raises aSyntaxError, IPython concludes the block cannot be validly extended (or was explicitly ended by a blank line) and immediately executes the cell so you see the traceback right away instead of getting stuck in an infinite...:loop.
- If
- Every time you press
- The 4 most common triggers inside IPython / Jupyter terminals:
- Pressing
Entertwice (or leaving a blank line with trailing spaces) inside anif/for/def/tryblock: IPython treats an empty unindented line as the signal to close and execute the block immediately, even if no body line was written yet (def foo():\n). - Pasting multi-line code with blank lines between methods or statements: In terminal IPython without bracketed-paste mode enabled, a blank line inside a pasted
classordefterminates the block halfway through, causing the first half to fail withunexpected EOF while parsing. - Custom
%magicor!shellsyntax mixed into incomplete Python blocks: IPython’s input transformer rewrites!cmdand%magiclines intoget_ipython().system(...)calls before compiling; placing an unindented!or%line inside an openforloop prematurely closes the indented block. - Running
exec()oreval()on truncated code strings inside IPython: Callingexec("if x > 0:")directly bypasses IPython’s multi-line prompt and passes the incomplete string straight to Python’s parser, raisingunexpected EOF while parsingimmediately.
- Pressing
- How to force multi-line editing cleanly in terminal IPython:
- Press
Alt+Enter(orEscfollowed byEnter) to insert a literal newline without submitting the cell, or run%editto compose complex multi-line functions in your external editor and execute them cleanly upon saving.
- Press
Comparison of Multi-Line Input Handling in IPython
| Scenario / Input Pattern | What check_complete() Returns |
IPython REPL Behavior | How to Fix / Prevent |
|---|---|---|---|
def f():\n x = 1\n (valid indented body + trailing newline) |
None (incomplete) |
Shows ...: continuation prompt and waits for more lines. |
Press Enter on an empty unindented line (or Ctrl+D / Alt+Enter) when finished. |
def f():\n (header followed immediately by an empty line) |
Raises SyntaxError (unexpected EOF / incomplete input) |
Executes immediately and displays SyntaxError. |
Keep cursor indented (�) and write at least one body statement (or pass) before pressing Enter twice. |
| Pasting code containing internal blank lines into a terminal | Prematurely closes block at the first blank line | Splits paste into multiple cells; first cell fails with unexpected EOF. |
Use a terminal supporting bracketed paste, or run %paste / %cpaste (finish with --). |
exec("for i in range(5):") inside a cell |
N/A (evaluated at runtime by exec()) |
Raises SyntaxError: unexpected EOF while parsing at runtime. |
Ensure dynamic code strings passed to exec()/eval() contain complete indented block bodies. |
You must log in or register to comment.
