SyntaxError: invalid syntax

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
14 min. read Python 3.7—3.11
Share
Copied to clipboard.
Tags

It's a sadly unhelpful error message and it's one that you'll see quite often when learning Python.

What does SyntaxError: invalid syntax mean? What is Python trying to tell you with this error and how can you fix your code to make Python happy?

What is a SyntaxError in Python?

This is Python's way of saying "I don't understand you". Python knows that what you've typed isn't valid Python code but it's not sure what advice to give you.

When you're lucky, your SyntaxError will have some helpful advice in it:

$ python3 greet.py
  File "/home/trey/greet.py", line 10
    if name == "Trey"
                     ^
SyntaxError: expected ':'

But if you're unlucky, you'll see the message invalid syntax with nothing more:

$ python3.9 greet.py
  File "/home/trey/greet.py", line 4
    name
    ^
SyntaxError: invalid syntax

This error message gives us no hints as to what might be going on outside of a line number and a bit of highlighting indicating where Python thinks the error occurred.

Causes of SyntaxError: invalid syntax

What are the likely causes of this mysterious error message?

When my Python students hit this error, the most likely causes are typically:

  1. Missing a colon (:) at the end of a line or mixing up other symbols
  2. Missing opening or closing parentheses ((...)), brackets ([...]), braces ({...}), or quotes ("...")
  3. Misspelled or missing keywords or mistyping syntax within a block or expression
  4. Attempting to use a reserved keyword as a variable name
  5. Incorrectly indented code or other whitespace errors
  6. Treating statements like expressions
  7. Copying Python code into the REPL or copying from the REPL into a Python file

That's a lot of options and they're not the only options. How should you approach fixing this problem?

Fixing SyntaxError: invalid syntax

The first step in fixing SyntaxErrors is narrowing down the problem.

I usually take the approach of:

  1. Note the line number and error message from the traceback, keeping in mind that both of these just guesses that Python's making
  2. Working through the above list of common causes
  3. Attempting to read the code as Python would, looking for syntax mistakes
  4. Narrowing down the problem by removing blocks of code that I suspect may be the culprit

It's easier to address that dreaded SyntaxError: invalid syntax exception when you're familiar with its most common causes. Let's attempt to build up our intuitions around SyntaxError: invalid syntax by touring the common causes of this error message.

Upgrading Python improves error messages

Before diving into specific errors, note that upgrading your Python version can drastically improve the helpfulness of common error messages.

Take this error message:

$ python3 square.py
  File "/home/trey/square.py", line 2
    return [
    ^
SyntaxError: invalid syntax

On Python 3.10 it looks considerably different:

$ python3.10 square.py
  File "/home/trey/square.py", line 1
    def square_all(numbers:
                  ^
SyntaxError: '(' was never closed

We're running the same square.py file in both cases:

def square_all(numbers:
    return [
        n**2
        for n in numbers
    ]

But Python 3.10 showed a much more helpful message: the line number, the position, and the error message itself are all much clearer for many unclosed parentheses and braces on Python 3.10 and above.

The dreaded missing colon is another example. Any expression that starts a new block of code needs a : at the end.

Here's the error that Python 3.9 shows for a missing colon:

$ python3 greet.py
  File "/home/trey/greet.py", line 10
    if name == "Trey"
                     ^
SyntaxError: invalid syntax

And here's the same error in Python 3.10:

$ python3 greet.py
  File "/home/trey/greet.py", line 10
    if name == "Trey"
                     ^
SyntaxError: expected ':'

Much more helpful, right? It's still a SyntaxError exception, but the message is much clearer than simply invalid syntax.

Python 3.10 also includes friendlier error messages when you use = where you likely meant ==:

>>> name = "Trey"
>>> if name = "Trey":
  File "<stdin>", line 1
    if name = "Trey":
       ^^^^^^^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

Newer Python versions include more helpful error messages for missing commas, inline if expressions, unclosed braces/brackets/parentheses, and more.

If you have the ability to run your code on a newer version of Python, try it out. It might help you troubleshoot your syntax errors more effectively.

Count your parentheses

Forgetting closing parentheses, brackets, and braces is also a common source of coding errors.

Fortunately, recent Python versions have started noting unclosed brackets in their SyntaxError messages.

When running this code:

def colors():
    c = ['red',
        'blue',
    return c

Python 3.9 used to show simply invalid syntax:

$ python3.9 colors.py
  File "/home/trey/colors.py", line 4
    return c
    ^
SyntaxError: invalid syntax

But Python 3.10 shows a more helpful error message:

$ python3.10 colors.py
  File "/home/trey/colors.py", line 2
    c = ['red',
        ^
SyntaxError: '[' was never closed

But sometimes Python can get a little confused when guessing the cause of an error. Take this populate.py script:

import random
import names

random_list=["a1","a2","a3","b1","b2","b3","c1","c2","c3"]

with open("one.txt","w+") as one, \
     open("two.txt","w+") as two, \
     open("three.txt","w+") as three:
      for i in range(0,3):
          one.write("%s\n" "%s" % (names.get_first_name(),
              random.choice(random_list))
          two.write("%s\n" "%s" % (names.get_first_name(),
              random.choice(random_list))
          three.write("%s\n" "%s" % (names.get_first_name(),
              random.choice(random_list))

When running this script, Python 3.10 shows this error message:

$ python3.10 populate.py
  File "/home/trey/populate.py", line 9
    one.write("%s\n" "%s" %
              ^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

The problem is that all 3 of those write method calls are missing closing parentheses.

Count your parentheses!

Many code editors highlight matching parentheses, brackets, and braces (when your cursor is at an opening parentheses the closing one will change color). Use your code editor to see if each pair of parenthesis matches up properly (and that the one in matches seems correct).

Misspelled, missing, or misplaced keywords

Can you see what's wrong with this line of code?

>>> drf sum_of_squares(numbers):
  File "<stdin>", line 1
    drf sum_of_squares(numbers):
        ^
SyntaxError: invalid syntax

We were trying to define a function, but we misspelled def as drf. Python couldn't figure out what we were trying to do, so it showed us that generic invalid syntax message.

What about this one?

>>> numbers = [2, 1, 3, 4, 7, 11, 18]
>>> squares = [n**2 for numbers]
  File "<stdin>", line 1
    squares = [n**2 for numbers]
                               ^
SyntaxError: invalid syntax

Python's pointing to the end of our comprehension and saying there's a syntax error. But why?

Look a bit closer. There's something missing in our comprehension.

We meant type this:

>>> numbers = [2, 1, 3, 4, 7, 11, 18]
>>> squares = [n**2 for n in numbers]

We were missing the in in our comprehension's looping component.

Misspelled keywords and missing keywords often result in that mysterious invalid syntax, but extra keywords can cause trouble as well.

You can't use reserved words as variable names in Python:

>>> class = "druid"
  File "<stdin>", line 1
    class = "druid"
          ^
SyntaxError: invalid syntax

Python sees that word class and it assumes we're defining a class. But then it sees an = sign and gets confused and can't tell what we're trying to do, so it throws its hands in the error and yells invalid syntax!

This one is a bit more mysterious:

>>> import urlopen from urllib.request
  File "<stdin>", line 1
    import urlopen from urllib.request
                   ^^^^
SyntaxError: invalid syntax

That looks right, doesn't it? So what's the problem?

In Python the import...from syntax is actually a from...import syntax. We meant to write this instead:

>>> from urllib.request import urlopen

Watch out for misspelled keywords, missing keywords, and re-arranged syntax. Also be sure not to use reserved words as variable names (e.g. class, return, and import are invalid variable names).

Subtle spacing problems

Can you see what's wrong in this code?

>>> class Thing:
...     def __init _(self, name, color):
  File "<stdin>", line 2
    def __init _(self, name, color):
               ^
SyntaxError: invalid syntax

Notice the extra space in the function name (__init_ _ instead of __init__)?

Can you identify what's wrong in this line?

>>> class Thing:
...     def__init__(self, name, color):
  File "<stdin>", line 2
    def__init__(self, name, color):
                                  ^
SyntaxError: invalid syntax

This one might be harder to spot.

Everything on that line is correct except that there's no space between def and __init__.

When one space character is valid, you can usually use more than one space character as well. But adding an extra space in the middle of an identifier or removing spaces where there should be spaces can often cause syntax errors.

Forgotten quotes and extra quotes

If you code infrequently, you likely forget to put quotes around your strings often. This is a very common mistake, so rest assured that you're not alone!

Forgotten quotes can sometimes result in this cryptic invalid syntax error message:

>>> print(Four!) if n
  File "<stdin>", line 1
    print(Four!) if n
              ^
SyntaxError: invalid syntax

Be careful with quotes within quotes:

>>> question = 'What's that?'
  File "<stdin>", line 1
    question = 'What's that?'
                     ^
SyntaxError: invalid syntax

You'll need to switch to a different quote style (using double quotes for example) or escape those quotes.

Mixing up your symbols

Sometimes your syntax might look correct but you've actually confused one bit of syntax for another common bit of syntax.

That's what happened here:

>>> things = {duck='purple', monkey='green'}
  File "<stdin>", line 1
    things = {duck='purple', monkey='green'}
              ^^^^^^^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

We're trying to make a dictionary and we've accidentally used = instead of : to separate our key-value pairs.

Here's another dictionary symbol mix up:

>>> states = [
...     'Oregon': 'OR',
  File "<stdin>", line 2
    'Oregon': 'OR',
            ^
SyntaxError: invalid syntax

It looks like we're trying to define a dictionary, but we started with an open square bracket ([) instead of an open curly brace ({).

Another common syntax mistake is missing periods:

  File "/home/trey/file_info.py", line 15
    size = path.stat()st_size
                      ^^^^^^^
SyntaxError: invalid syntax

We're trying to access the st_size attribute on the object returned from that path.stat() call, but we've forgot to put a . before st_size.

Sometimes syntax errors are due to characters being swapped around:

>>> name = "Trey"
>>> name[]0
  File "<stdin>", line 1
    name[]0
         ^
SyntaxError: invalid syntax

And some syntax errors are due to extra symbols you didn't intend to write:

>>> name = "Trey"
>>> name.lower.()
  File "<stdin>", line 1
    name.lower.()
               ^
SyntaxError: invalid syntax

We wrote an extra . before our parentheses above.

Indentation errors in disguise

Sometimes a SyntaxError is actually an indentation error in disguise.

For example this code has an else clause that's too far indented:

import sys

name = sys.argv[1]

if name == "Trey":
    print("I know you")
    print("Your name is Troy... no, Trent? Trevor??")
    else:
        print("Hello stranger")

When we run the code we'll see a SyntaxError:

$ python greet.py
  File "/home/trey/greet.py", line 8
    else:
    ^^^^
SyntaxError: invalid syntax

Indentation issues often result in IndentationError exceptions, but sometimes they'll manifest as SyntaxError exceptions instead.

Embedding statements within statements

A "statement" is either a block of Python code or a single line of Python code that can stand on its own.

An "expression" is a chunk of Python code that evaluates to a value. Expressions contain identifiers (i.e. variables), literals (e.g. [1, 2], "hi", and 4), and operators (e.g. +, in, and *).

In Python we can embed one expression within another. But some expressions are actually "statements" which must be a line all on their own.

Here we've tried to embed one statement within another:

>>> def square_all(numbers):
...     return result = [n**2 for n in numbers]
  File "<stdin>", line 2
    return result = [n**2 for n in numbers]
                  ^
SyntaxError: invalid syntax

Assignments are statements in Python (result = ... is a statement). Python's return is also a statement. We've tried to embed one statement inside another and Python didn't understand us.

We likely meant either this:

def square_all(numbers):
    result = [n**2 for n in numbers]
    return result

Or this:

def square_all(numbers):
    return [n**2 for n in numbers]

Here's the same issue with the global statement (see assigning to global variables):

>>> def connect(*args, **kwargs):
...     global _connection = sqlite3.connect(*args, **kwargs)
  File "<stdin>", line 2
    global _connection = sqlite3.connect(*args, **kwargs)
                       ^
SyntaxError: invalid syntax

And the same issue with the del statement:

trey_count = del counts['Trey']
  File "<stdin>", line 1
    trey_count = del counts['Trey']
                 ^^^
SyntaxError: invalid syntax

If assignment is involves in your statement-inside-a-statement, an assignment expression (via Python's walrus operator) may be helpful in resolving your issue. Though often the simplest solution is to split your code into multiple statements over multiple lines.

Errors that appear only in the Python REPL

Some errors are a bit less helpful within the Python REPL.

Take this invalid syntax error:

def square_all(numbers:
    return [
  File "<stdin>", line 2
    return [
    ^^^^^^
SyntaxError: invalid syntax

We'll see that error within the Python REPL even on Python 3.11.

The issue is that the first line doesn't have a closing parentheses. Python 3.10+ would properly point this out if we ran our code from a .py file instead:

$ python3.10 square.py
  File "/home/trey/square.py", line 1
    def square_all(numbers:
                  ^
SyntaxError: '(' was never closed

But at the REPL Python doesn't parse our code the same way (it parses block-by-block in the REPL) and sometimes error messages are a bit less helpful within the REPL as a result.

Here's another REPL-specific error:

>>> def greet(name):
...     print(f"Howdy {name}!")
... greet("Trey")
  File "<stdin>", line 3
    greet("Trey")
    ^^^^^
SyntaxError: invalid syntax

This is valid Python code:

def greet(name):
    print(f"Howdy {name}!")
greet("Trey")

But that can't be copy-pasted directly into the REPL. In the Python REPL a blank line is needed after a block of code to end that block.

So we'd need to put a newline between the function definition and the function call:

>>> def greet(name):
...     print(f"Howdy {name}!")
...
>>> greet("Trey")
Howdy Trey!

Some errors are due to code that feels like it should work in a Python REPL but doesn't. For example running python from within your Python REPL doesn't work:

>>> python greet.py
  File "<stdin>", line 1
    python greet.py
           ^^^^^
SyntaxError: invalid syntax
>>> python -m pip install django
  File "<stdin>", line 1
    python -m pip install django
              ^^^
SyntaxError: invalid syntax

The above commands would work from our system command-prompt, but they don't work within the Python REPL.

If you're trying to launch Python or send a command to your prompt outside Python (like ls or dir), you'll need to do it from your system command prompt (Terminal, Powershell, Command Prompt, etc.). You can only type valid Python code from within the Python REPL.

Problems copy-pasting from the REPL

Copy-pasting from the Python REPL into a .py file will also result in syntax errors.

Here's we're running a file that has >>> prefixes before each line:

$ python numbers.py
  File "/home/trey/numbers.py", line 1
    >>> n = 4
    ^^
SyntaxError: invalid syntax

This isn't a valid Python program:

>>> n = 4
>>> print(n**2)

But this is a valid Python program:

n = 4
print(n**2)

You'll need to be careful about empty lines when copy-pasting from a .py file into a Python REPL and you'll need to be careful about >>> and ... prefixes and command output when copy-pasting from a REPL into a .py file.

The line number is just a "best guess"

It used to be that the line number for an error would usually represent the place that Python got confused about your syntax. That line number was often one or more lines after the actual error.

In recent versions of Python, the core developers have updated these line numbers in an attempt to make them more accurate.

For example here's an error on Python 3.9 due to a missing comma:

$ python3.9 greet.py
  File "/home/trey/greet.py", line 4
    name
    ^
SyntaxError: invalid syntax

And here's the same error in Python 3.10:

$ python3.10 greet.py
  File "/home/greet.py", line 3
    "Hello there"
    ^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

Python's given us a helpful hint on Python 3.10. But it's also made a different guess about what line the error is on.

As you can see from the greet.py file, line 3 ("Hello there") is the better guess in this case, as that's where the comma is needed.

def greet(name):
    print(
        "Hello there"
        name
    )

While deciphering tracebacks, keep in mind that the line number is just Python's best guess as to where the error occurred.

SyntaxError exceptions happen all the time

If your code frequently results in SyntaxError exceptions, don't fret. These kinds of exceptions happen all the time. When you're newer to Python, you'll find that it's often a challenge to remember the exact syntax for the statements you're writing.

But more experienced Python programmers also experience syntax errors. I make typos in my code quite often. I have a linter installed in my text editor to help me catch those typos though. I recommend searching for a "Python" or "Python linter" extension for your favorite code editor so you can spot these issues quickly every time you save your .py files.

Once you get past syntax errors, you'll likely hit other types of exceptions. Watch the exception screencast series for more on reading tracebacks and exception handling in Python.

Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.