Skip to content Skip to sidebar Skip to footer

ImportError: Cannot Import Name MainClass

society. I'm trying to understand the OOP programming and I'm facing some issues and asking for help. Here the example: I'm trying to create all objects under one class and then I

Solution 1:

I changed your code to this:

baseclass.py

import first
import second

class MainClass:
    def __init__(self):
        self.firstclass = first.FirstClass()
        self.secondclass = second.SecondClass()

first.py

import baseclass

class FirstClass(baseclass.MainClass):
    def __init__(self):
        baseclass.MainClass.__init__(self)

    def add_two_number(self):
        return 2 + 2

second.py

import baseclass

class SecondClass():
    def __init__(self):
        baseclass.MainClass.__init__(self)

    def minus_number(self):
        return self.firstclass.add_two_number() - 10

if __name__ == '__main__':
    print(second.SecondClass().minus_number())

I get no ImportErrors or any errors of any kind. I think your ImportError had something to do with from instead of just import. I hope this helps.


Post a Comment for "ImportError: Cannot Import Name MainClass"