ImportError: cannot import the class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nischalinn
    New Member
    • Mar 2014
    • 16

    #1

    ImportError: cannot import the class

    I can not figure out what mistake I've done with my code. The error indicates that the class can not be imported. But I am not getting into it.

    Please help me with this.

    My code:
    Code:
    ######## userClass.py ##################
    from classTriangle import classTriangle
    
    class userClass():
        
        def __init__(self):
            self.getList = []
            self.maxList = 3
            
        
        def getUserList(self):
            print("*** getting user input ***")
            while len(self.getList) < self.maxList:
                item = input("enter numbers: ")
                self.getList.append(item)
            return self.getList    
            
        def showUserList(self,passedList):
            print("*** showing user input ***")
            print (passedList)
            print ("length of the list: ", len(passedList))
            
            
    if __name__=='__main__':
        ui = userClass()
        mk = classTriangle()
        displayList = ui.getUserList()
        ui.showUserList(displayList)    
        print(mk.createTriangle())
               
            
    ########classTriangle.py############
    from userClass import userClass 
    
    class classTriangle(): 
            
        def __init__(self):
            self.makeList = []
            self.bridgeList = []
            self.finalList = []   
        
        def createTriangle(self):
            UI = userClass()
            self.makeList = UI.getUserList()
            while len(self.makeList)>1:
                for i in range(len(self.makeList)):
                    j = i + (i+1)
                    self.bridgeList.append(j)
                del self.makeList[:]
                self.makeList.extend(self.bridgeList)  
                self.finalList.extend(self.bridgeList)
                del self.bridgeList[:]
            return self.finalList
    The error shown is:
    Code:
            
    Traceback (most recent call last):
      File "/home/gharry/workspace/myProject/makeTraingle/classTriangle.py", line 6, in <module>
        from userClass import userClass 
      File "/home/gharry/workspace/myProject/makeTraingle/userClass.py", line 6, in <module>
        from classTriangle import classTriangle
      File "/home/gharry/workspace/myProject/makeTraingle/classTriangle.py", line 6, in <module>
        from userClass import userClass 
    ImportError: cannot import name 'userClass'
    Please help.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    from userClass import userClass
    ImportError: cannot import name 'userClass'
    Because both the file and the class have the same name, there is no way to tell if the problem is with the program named userClass.py or the class named userClass within the userClass.py program. For starters see if the program is in the PYTHONPATH, i.e. your program can find this program, by inserting this
    Code:
    if os.path.isfile("userClass.py"):
        import userClass.py
    else:
        ## whatever you want
    See the Search Path here for info on expanding the search path on a per program basis. On a Linux system you can add an additional new path(s) to the PYTHONPATH variable in your ~.bashrc file.

    Comment

    • nischalinn
      New Member
      • Mar 2014
      • 16

      #3
      @dwblas:

      Thanks for your help. I've read the post. I've created a new class called "main" and have instantiate all the classes there.

      Thank You!!!

      Comment

      Working...