Common Python Errors

Common Python Errors

by boxplotanalytics    Sep 1, 2019 2:23 am  
This post is updated as appropriate, so keep checking back!
Table of Contents

Errors when installing python with homebrew Errors when writing/running python code

Installing Python

Error: Permission denied @ dir_s_mkdir – /usr/local/Frameworks

Check out this article for help: https://github.com/Homebrew/homebrew-core/issues/19286  

Error: Could not symlink bin/2to3

Check out this article for help: https://stackoverflow.com/questions/13354207/how-to-symlink-python-in-homebrew

Writing Python Code

TypeError: Can’t convert …

This means one of a few things:
  1. That you are trying to combine data types that aren’t compatible. You can’t concatenate a string and an integer for example.
  2. You’re trying to perform some operation on the wrong datatype for that function.
 

NameError: global name ‘—‘ is not defined

Did you forget to put something in quotes? Remember if you didn’t define something as a variable, list, dictionary, etc. previously, and it’s not a number, it needs to be in quotes!  

IndentationError: expected an indented block

There are several types of indentation errors. These are pretty self-explanatory. You either forgot an indent or have too many. Remember, python considers indents to be four spaces or a tab, exactly.  

SyntaxError: invalid syntax

This could mean a lot of things, but basically you aren’t following one of the basic syntax rules of Python. Here are some common examples:
  • Forgetting the parens around the arguments to print
  • Forgetting the colon at the end of the condition in an if statement, or in a for loop
  • Trying to use a reserved word as a variable name
  • Code like: if my_variable = 8: (should be == 8 when in an IF statement!)
 

IndexError: list index out of range

Typically this means you are trying to access an item in a list that doesn’t exist. For example, : flowers = ["rose", "tulip", "daisy"] print("Flowers in my garden are:", flowers[1], flowers[2], flowers[3]) There is no flowers[3]! Remember, lists start at 0, so it should have been flowers[0], flowers[1], flowers[2].  

KeyError

These seem scary, but they are similar to the NameError, only specific to dictionaries. They are raised when a key is not found in the set of existing keys. Check for spelling and case sensitivity!  

ValueError

This is most commonly caused by trying to convert a bad string into a number. For example: my_num = int("Word").

Need help applying these concepts to your data?

Chat with us about options.

Schedule a Meeting   


Continue to make data-driven decisions.

Sign up for our email guides that contains relevant tips, software tricks, and news from the data world.

*We never spam you or sell your information.

* indicates required

"Tables & Linking Data Structures in Excel"

"Population Mean Hypothesis Test Example"