Last modified: Sep 18, 2026

Python Class Not Defined Error

In Python, encountering a class not defined error can be confusing for beginners. This error typically occurs when you try to use a class before it has been properly defined or imported. In this article, we will explore the common causes of this error and provide practical solutions to resolve it.

Understanding the Error

The class not defined error is a TypeError or NameError that happens when Python cannot find a class definition in the current scope. This often occurs when:

  • The class has not been defined yet.
  • The class name is misspelled.
  • The class is in a different module and has not been imported.

Example of the Error

Let's look at a simple example that triggers this error:


# Attempting to use a class before it is defined
print(MyClass())

NameError: name 'MyClass' is not defined

In the above code, we tried to create an instance of MyClass before defining it. As expected, Python raised a NameError because the class does not exist in the current scope.

Common Causes and Solutions

1. Defining the Class First

The most straightforward solution is to ensure that the class is defined before it is used. Here’s how you can fix the previous example:


# Define the class first
class MyClass:
    def __init__(self):
        self.message = "Hello, World!"

# Now create an instance
obj = MyClass()
print(obj.message)

Hello, World!

By defining MyClass before using it, we avoid the class not defined error. The output confirms that the class was successfully instantiated and its attribute was printed.

2. Correct Spelling of Class Names

Another common cause of this error is a typo in the class name. Python is case-sensitive, so even a small mistake can lead to this issue.


class MyClass:
    def __init__(self):
        self.message = "Hello, World!"

# Typo in class name
obj = Myclass()
print(obj.message)

NameError: name 'Myclass' is not defined

In this example, we misspelled the class name as Myclass instead of MyClass. To fix this, simply correct the spelling:


class MyClass:
    def __init__(self):
        self.message = "Hello, World!"

# Correct spelling
obj = MyClass()
print(obj.message)

Hello, World!

3. Importing Classes from Other Modules

When working with multiple files or modules, you might encounter the class not defined error if you forget to import the class. Let’s consider a scenario with two files:

File: example_module.py


class MyClass:
    def __init__(self):
        self.message = "Hello from MyClass!"

File: main.py


# Forgot to import MyClass
obj = MyClass()
print(obj.message)

NameError: name 'MyClass' is not defined

To resolve this error, you need to import the class from the module:


# Import the class from the module
from example_module import MyClass

obj = MyClass()
print(obj.message)

Hello from MyClass!

Now, the class is properly imported, and the program runs without errors.

Best Practices to Avoid Class Not Defined Errors

Following best practices can help you avoid encountering the class not defined error in your Python programs:

  • Define Classes Early: Always define your classes at the beginning of your script or in a separate module.
  • Use Consistent Naming: Stick to a consistent naming convention for your classes. PEP 8 recommends using CapWords for class names.
  • Organize Imports: Keep your imports at the top of your file and ensure all necessary classes are imported.
  • Test Incrementally: Test your code incrementally as you build it to catch errors early.

Conclusion

The class not defined error in Python is a common issue that can be easily resolved by following a few best practices. By ensuring that classes are defined before use, checking for typos, and properly importing classes from other modules, you can avoid this error and write more robust Python code. Remember to test your code regularly and maintain good coding habits to prevent such issues in the future.