Error while using a custom class in Python: 'AttributeError: 'module' object has no attribute 'MyClass

288 Views Asked by At

I'm facing an issue while working with a custom class in Python. I have a module named 'my_module' with a class called 'MyClass'. However, when I try to instantiate the class in another script or module, I get the following error:

AttributeError: 'module' object has no attribute 'MyClass'

Here's a simplified version of my code:
# my_module.py

class MyClass:
    def __init__(self, value):
        self.value = value
# main.py
from my_module import MyClass

obj = MyClass(42)



I have checked the module and class names, and they seem to be correct. What could be causing this error, and how can I resolve it? Any help or guidance would be appreciated. Thank you !
1

There are 1 best solutions below

0
On

We can't reproduce your error with the code you provided.

Looking at your error: You may have forgot to create an init file __init__.py at the root of your module folder. Or you may have done a typo somewhere.