Last modified: Sep 18, 2026

Fix Get Is Not Defined Python Error

In Python, encountering the error "get is not defined" can be confusing for beginners. This error typically occurs when you try to use the get method on a dictionary or another object without properly defining or importing it. Let's explore what causes this error and how to fix it.

What Causes the "Get Is Not Defined" Error?

Python raises a NameError when you reference a variable or function that hasn't been defined yet. If you see "get is not defined", it means Python cannot find a function or method named get in the current scope.

This often happens when working with dictionaries. Dictionaries have a built-in method called get, which retrieves the value for a given key. However, if you accidentally call get as a standalone function instead of using it as a method, Python will throw an error.

Incorrect Usage Example


# Trying to use get as a function instead of a dictionary method
my_dict = {"name": "Alice", "age": 30}
value = get(my_dict, "name")  # This will cause an error
print(value)

NameError: name 'get' is not defined

As shown in the output above, Python doesn't know what get refers to because it was used incorrectly. The get method belongs to the dictionary object, not the global scope.

How to Fix the Error

To resolve the "get is not defined" error, always use get as a method on the dictionary object. Here's the correct way:

Correct Usage Example


# Using get as a dictionary method
my_dict = {"name": "Alice", "age": 30}
value = my_dict.get("name")  # Correct usage
print(value)

Alice

By calling my_dict.get("name"), you're telling Python to look up the key "name" in the dictionary my_dict. This avoids the NameError entirely.

Understanding the Dictionary Get Method

The get method is very useful for safely accessing dictionary values. Unlike direct indexing (my_dict["key"]), which raises a KeyError if the key doesn't exist, get returns None by default when the key is missing.


# Safe access with get method
my_dict = {"name": "Alice", "age": 30}

# Key exists
print(my_dict.get("name"))  # Output: Alice

# Key does not exist
print(my_dict.get("city"))  # Output: None

# Custom default value
print(my_dict.get("city", "Unknown"))  # Output: Unknown

Alice
None
Unknown

Using get prevents your program from crashing when a key is missing. It's considered a best practice for dictionary access in Python.

Common Mistakes Leading to the Error

Besides using get as a standalone function, there are other common mistakes that lead to the "get is not defined" error:

1. Using Get Outside of a Dictionary Context


# Trying to use get on a list
my_list = [1, 2, 3]
value = my_list.get(0)  # Lists don't have a get method

AttributeError: 'list' object has no attribute 'get'

Lists do not support the get method. Use indexing instead:


my_list = [1, 2, 3]
value = my_list[0]  # Correct way to access list elements
print(value)

1

2. Forgetting to Define the Dictionary


# Using get before defining the dictionary
value = my_dict.get("name")  # my_dict is not defined yet

NameError: name 'my_dict' is not defined

Always make sure the dictionary is defined before calling get on it.

Using Get with Other Objects

Some objects in Python also support the get method. For example, the requests library uses get to make HTTP requests. Make sure to import the correct module:


import requests

# Correct usage with requests
response = requests.get("https://www.example.com")
print(response.status_code)

200

If you forget to import requests, you'll get a NameError. Always check your imports when using external libraries.

Best Practices to Avoid the Error

Follow these tips to prevent the "get is not defined" error:

  • Always use get as a method on the appropriate object, like dictionaries.
  • Check object types before calling methods. Not all objects support get.
  • Import required modules when using functions like requests.get.
  • Use IDE warnings to catch undefined names early.

Conclusion

The "get is not defined" error in Python is usually caused by incorrect usage of the get method. By understanding how and where get works, you can avoid this common mistake. Remember to use get only as a method on dictionaries or supported objects, and always ensure proper imports when working with external libraries.

With practice, you'll quickly recognize and fix this error. Keep experimenting with Python, and refer back to this guide whenever you encounter naming or method-related issues.