SyntaxError: invalid syntax
Updated
The Python error "SyntaxError: invalid syntax" marks where the parser gave up, often just after the mistake. The usual causes, and what newer Pythons say.
What you see
whille True:
print("tick") File "/home/me/project/main.py", line 1
whille True:
^^^^^^
SyntaxError: invalid syntax. Did you mean 'while'?What it means
The parser reached a point where no valid Python could continue, and stopped. The caret under the line shows where it stopped, which is the first place it noticed, not always where the mistake is. Look at the marked spot, then at what comes before it, including the previous line.
"Invalid syntax" is the parser's last resort. Since Python 3.10 most common mistakes get a specific message instead, and Python 3.14 suggests the keyword you probably meant, as in this example.
Why it happens
- A misspelled keyword, such as
whille,frooresle. - A missing comma between items in a list, a tuple or a call. From 3.10 the message adds "Perhaps you forgot a comma?".
- A shell command in a Python file or at the
>>>prompt.pip install requestsandpython main.pyare commands for the terminal, not Python code. =where==was meant, inside anif.- Python 2 code, such as
print "hello". Python 3 answers with "Missing parentheses in call to 'print'". - An unclosed bracket or quote on an earlier line, which makes the parser read on into the next one.
How to fix it
Read the caret, then the line above it. If the line looks fine, check that every bracket and quote opened before it is closed.
For a command such as pip install, leave Python first. At the >>> prompt type exit(), then run the command in the terminal:
python3 -m pip install requests
Running a recent Python helps by itself: 3.10 and later replace many "invalid syntax" errors with a message that names the problem.
Example: a missing comma
point = (3 4) File "/home/me/project/main.py", line 1
point = (3 4)
^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?| Python | Prints |
|---|---|
| 3.9 | SyntaxError: invalid syntax |
| 3.10–3.15 | SyntaxError: invalid syntax. Perhaps you forgot a comma? |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.
Example: a terminal command in a Python file
pip install requests File "/home/me/project/main.py", line 1
pip install requests
^^^^^^^
SyntaxError: invalid syntax| Python | Prints |
|---|---|
| 3.9–3.15 | SyntaxError: invalid syntax |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.
How each Python version words it
| Python | Prints |
|---|---|
| 3.9–3.13 | SyntaxError: invalid syntax |
| 3.14–3.15 | SyntaxError: invalid syntax. Did you mean 'while'? |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.
The keyword suggestion is new in 3.14. The missing-comma hint dates from 3.10. A shell command still gets the plain "invalid syntax" everywhere.
Questions
Why is the error on a line that looks correct?
The mistake is usually earlier. An unclosed bracket on line 3 can make Python report line 4, because it only finds out the bracket was never closed when the next statement starts.
Does a SyntaxError run any of my code?
No. The whole file is parsed before any of it runs, so nothing above the error runs either.
Sources
- Built-in exceptions: SyntaxError, Python documentation
- What's New in Python 3.10: SyntaxErrors, Python documentation
- What's New in Python 3.14: improved error messages, Python documentation
Read on October 25, 2026.