Skip to main content
Engineering LibreTexts

20.1: Syntax errors

  • Page ID
    40857
  • Syntax errors are usually easy to fix once you figure out what they are. Unfortunately, the error messages are often not helpful. The most common messages are SyntaxError: invalid syntax and SyntaxError: invalid token, neither of which is very informative.

    On the other hand, the message does tell you where in the program the problem occurred. Actually, it tells you where Python noticed a problem, which is not necessarily where the error is. Sometimes the error is prior to the location of the error message, often on the preceding line.

    If you are building the program incrementally, you should have a good idea about where the error is. It will be in the last line you added.

    If you are copying code from a book, start by comparing your code to the book’s code very carefully. Check every character. At the same time, remember that the book might be wrong, so if you see something that looks like a syntax error, it might be.

    Here are some ways to avoid the most common syntax errors:

    1. Make sure you are not using a Python keyword for a variable name.
    2. Check that you have a colon at the end of the header of every compound statement, including for, while, if, and def statements.
    3. Make sure that any strings in the code have matching quotation marks. Make sure that all quotation marks are “straight quotes”, not “curly quotes”.
    4. If you have multiline strings with triple quotes (single or double), make sure you have terminated the string properly. An unterminated string may cause an invalid token error at the end of your program, or it may treat the following part of the program as a string until it comes to the next string. In the second case, it might not produce an error message at all!
    5. An unclosed opening operator—(, {, or [—makes Python continue with the next line as part of the current statement. Generally, an error occurs almost immediately in the next line.
    6. Check for the classic = instead of == inside a conditional.
    7. Check the indentation to make sure it lines up the way it is supposed to. Python can handle space and tabs, but if you mix them it can cause problems. The best way to avoid this problem is to use a text editor that knows about Python and generates consistent indentation.
    8. If you have non-ASCII characters in the code (including strings and comments), that might cause a problem, although Python 3 usually handles non-ASCII characters. Be careful if you paste in text from a web page or other source.

    If nothing works, move on to the next section...

    I keep making changes and it makes no difference.

    If the interpreter says there is an error and you don’t see it, that might be because you and the interpreter are not looking at the same code. Check your programming environment to make sure that the program you are editing is the one Python is trying to run.

    If you are not sure, try putting an obvious and deliberate syntax error at the beginning of the program. Now run it again. If the interpreter doesn’t find the new error, you are not running the new code.

    There are a few likely culprits:

    • You edited the file and forgot to save the changes before running it again. Some programming environments do this for you, but some don’t.
    • You changed the name of the file, but you are still running the old name.
    • Something in your development environment is configured incorrectly.
    • If you are writing a module and using import, make sure you don’t give your module the same name as one of the standard Python modules.
    • If you are using import to read a module, remember that you have to restart the interpreter or use reload to read a modified file. If you import the module again, it doesn’t do anything.

    If you get stuck and you can’t figure out what is going on, one approach is to start again with a new program like “Hello, World!”, and make sure you can get a known program to run. Then gradually add the pieces of the original program to the new one.

    • Was this article helpful?