0

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
  • 2
    I have tried your code and cannot reproduce the error. How does your folder structure look like? Commented Nov 30, 2023 at 14:04

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.