Common Python Errors

Below are a few common error messages that you will likely encounter as you first learn Python and long afterward. No matter how much you know, you will always encounter errors!

To learn more about these and other Python errors, see Python’s official documentation.

SyntaxError

A SyntaxError means that something has gone wrong with your Python syntax, aka the arrangement of words and punctuation in your code. Often, as below, this error will result from forgetting a closing quotation mark in a string or from forgetting a colon in a for loop.

print("Hope this goes off without a hitch!)
  File "<ipython-input-1-19e53e94c0d4>", line 1
    print("Hope this goes off without a hitch!)
                                               ^
SyntaxError: EOL while scanning string literal
for item in items
    print(item)
  File "<ipython-input-23-8d2ad4100244>", line 1
    for item in items
                     ^
SyntaxError: invalid syntax

The error message will often include a caret or arrow that points to the problematic part of the code:

FileNotFound Error

A FileNotFoundError means that whatever file name you’ve typed in cannot be located. Often, this error will result from simple typos in the file name or from not pointing to the correct directory which contains the file. Double check your spelling and where your desired file is located relative to your Python code.

open('../Wrong-Directory/File-Name.txt').read()
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-11-02c3b07d9ef8> in <module>()
----> 1 open('../Wrong-Directory/File-Name.txt').read()

FileNotFoundError: [Errno 2] No such file or directory: '../Wrong-Directory/File-Name.txt'

TypeError

A TypeError means that you’re trying to perform a function or operation on something that is not the correct data type for that function or operation. For example, if you try to divide by a variable that is a string, rather than an integer or float (as in the example below), a TypeError will be thrown. Double check your data types with the type() function.

favorite_artist = "Beyoncé"
favorite_artist / 2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-905fd552537b> in <module>()
----> 1 favorite_artist / 2

TypeError: unsupported operand type(s) for /: 'str' and 'int'
type(favorite_artist)
str

NameError

A NameError means that the variable name that you’re using cannot be found. Often, this error results from forgetting to run the cell that defines your variable or from misspelling the name of your variable, as below. Check your spelling and make sure you’ve run all necessary cells.

favorite_artist = "Beyoncé"
favorite_arteest
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-12-e9f56c66e444> in <module>()
----> 1 favorite_arteest

NameError: name 'favorite_arteest' is not defined

AttributeError

An AttributeError means that you’re trying to access something from an object that that object doesn’t possess or do something with an object that that object cannot do. For example, to transform the variable favorite_artist (which contains the string “Beyoncé”) from title case to uppercase, we can run favorite_arist.upper() because .upper() is a built-in string method. But if we forget the name of that string method and instead type .uppercase(), which is not an existing string method, then an AttributeError will be raised.

favorite_artist.upper()
'BEYONCÉ'
favorite_artist.uppercase()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-24-d061089bd6d4> in <module>()
----> 1 favorite_artist.uppercase()

AttributeError: 'str' object has no attribute 'uppercase'