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
  • what is the structure of your project (including filenames)? how do you run it? – Hagai Oct 25 '18 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. – BoboDarph Oct 25 '18 at 11:40
  • The .py files are all in the same folder(Calculation). I'm running though command line –  Oct 25 '18 at 11:41
  • @BoboDarph not accurate, if the filename is Class1 this should work – Hagai Oct 25 '18 at 11:42
  • @Heredity what are the full names of the files? and what is the exact command that you are using? – Hagai Oct 25 '18 at 11:42
  • @Hagai there is no reference to a file named Class1 in his post. Just two classes and a bad import statement. – BoboDarph Oct 25 '18 at 11:44
  • right @BoboDarph, that's why I ask what are the filenames. If the files are called `Class1.py` and `Class2.py`, then this should work. – Hagai Oct 25 '18 at 11:45
  • @Hagai The class names match the file names, im just using #! C:\Soft\Python\python.exe inside the file and dragging the file into the command console –  Oct 25 '18 at 11:48
  • @Heredity so they are called `Class1.py` and `Class2.py`, right? you still haven't answered my question how exactly you run it. specifically - what is the working directory from which you are running it. – Hagai Oct 25 '18 at 11:49
  • https://stackoverflow.com/questions/41276067/importing-class-from-another-file – Govardhan Bhure Jul 24 '20 at 09:09

1 Answers1

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

Boldewyn
  • 81,211
  • 44
  • 156
  • 212
Avishay Cohen
  • 1,978
  • 2
  • 21
  • 34
  • 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 –  Oct 25 '18 at 11:55