0

Im trying to import Class1 into Class2 as below. Im getting the following error importError : cannot import name 'Class1'

class Class1: def __init__(self, list1, list2, list3): self.Var1= list1 self.Var2= list2 self.Var3= list3 self.Var4= list4 self.Var5= list5 self.Var6= list6 self.Var7= list7 

Class 2 below

from Class1 import Class1 class Class2(Class1): def __init__(self,class_Class1): self.Var1= class_Class1.list1 self.Var2= class_Class1.list2 self.Var3 = class_Class1.list3 self.Var4= class_Class1.list4 self.Var5= class_Class1.list5 self.Var6= class_Class1.list6 self.Var7= class_Class1. list7 self.A = A self.B = B self.C = C 
10
  • what is the structure of your project (including filenames)? how do you run it? Commented Oct 25, 2018 at 11:40
  • Class1 is not a member of Class1. You can't import a module that isn't defined. If you had a Class1 member of Class1, then you could import it from Class1. Commented Oct 25, 2018 at 11:40
  • The .py files are all in the same folder(Calculation). I'm running though command line Commented Oct 25, 2018 at 11:41
  • @BoboDarph not accurate, if the filename is Class1 this should work Commented Oct 25, 2018 at 11:42
  • @Heredity what are the full names of the files? and what is the exact command that you are using? Commented Oct 25, 2018 at 11:42

1 Answer 1

0

it should be:

from <file_name_of_class1> import Class1 

so if the file name is Class1.py the line you wrote should be correct. please check your file names and tell us, also you might need to have an empty file named: __init__.py in order to import. check out this guide: guide on import in python

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

1 Comment

Yes the file names match the Class names(double checked by copying the names into each other), I tried adding a empty init.py and get the same error. I am able to import other classes into Class1