By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

ImportError: cannot import the class

P: 16
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:
Expand|Select|Wrap|Line Numbers
  1. ######## userClass.py ##################
  2. from classTriangle import classTriangle
  3.  
  4. class userClass():
  5.  
  6.     def __init__(self):
  7.         self.getList = []
  8.         self.maxList = 3
  9.  
  10.  
  11.     def getUserList(self):
  12.         print("*** getting user input ***")
  13.         while len(self.getList) < self.maxList:
  14.             item = input("enter numbers: ")
  15.             self.getList.append(item)
  16.         return self.getList    
  17.  
  18.     def showUserList(self,passedList):
  19.         print("*** showing user input ***")
  20.         print (passedList)
  21.         print ("length of the list: ", len(passedList))
  22.  
  23.  
  24. if __name__=='__main__':
  25.     ui = userClass()
  26.     mk = classTriangle()
  27.     displayList = ui.getUserList()
  28.     ui.showUserList(displayList)    
  29.     print(mk.createTriangle())
  30.  
  31.  
  32. ########classTriangle.py############
  33. from userClass import userClass 
  34.  
  35. class classTriangle(): 
  36.  
  37.     def __init__(self):
  38.         self.makeList = []
  39.         self.bridgeList = []
  40.         self.finalList = []   
  41.  
  42.     def createTriangle(self):
  43.         UI = userClass()
  44.         self.makeList = UI.getUserList()
  45.         while len(self.makeList)>1:
  46.             for i in range(len(self.makeList)):
  47.                 j = i + (i+1)
  48.                 self.bridgeList.append(j)
  49.             del self.makeList[:]
  50.             self.makeList.extend(self.bridgeList)  
  51.             self.finalList.extend(self.bridgeList)
  52.             del self.bridgeList[:]
  53.         return self.finalList
  54.  
The error shown is:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Traceback (most recent call last):
  3.   File "/home/gharry/workspace/myProject/makeTraingle/classTriangle.py", line 6, in <module>
  4.     from userClass import userClass 
  5.   File "/home/gharry/workspace/myProject/makeTraingle/userClass.py", line 6, in <module>
  6.     from classTriangle import classTriangle
  7.   File "/home/gharry/workspace/myProject/makeTraingle/classTriangle.py", line 6, in <module>
  8.     from userClass import userClass 
  9. ImportError: cannot import name 'userClass'
  10.  
Please help.
Sep 29 '14 #1

✓ answered by dwblas

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
Expand|Select|Wrap|Line Numbers
  1. if os.path.isfile("userClass.py"):
  2.     import userClass.py
  3. else:
  4.     ## 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.

Share this Question
Share on Google+
2 Replies


Expert 100+
P: 621
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
Expand|Select|Wrap|Line Numbers
  1. if os.path.isfile("userClass.py"):
  2.     import userClass.py
  3. else:
  4.     ## 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.
Sep 29 '14 #2

P: 16
@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!!!
Nov 3 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.