Cloud Stack Ninja

I have a question about an issue with importing modules/packages that I cant seem to wrap my head around if anyone could clear up my confusion. I have a main folder called parent_directory Inside this I have two packages, package1 contains the modules, module1, module2 and a folder called package2, the subfolder package2 contains the module, module3

package,module structure

module1.py contains

class module1:
print("I am MODULE 1 in PACKAGE 1")

module2.py contains

class module1:
print("I am MODULE 2 in PACKAGE 1")

module3.py contains

class module3:
print("I am MODULE 3 in PACKAGE 2")

When I open OOP lab3.py and use the following

from package1.module1 import module1
module1()

The printout is, I am MODULE 1 in PACKAGE 1 which is what I expect, however, if I change this to

from package1.module2 import module2
module2()

The printout is, I am MODULE 2 in PACKAGE 1 which again is what I expect, but I also get the error

File "D:/Docs/Programming/Python/parent_directory/OOP lab 3.py", line 8, in from package1.module2 import module2 ImportError: cannot import name 'module2' from 'package1.module2' (D:\Docs\Programming\Python\parent_directory\package1\module2.py)

If module 1 & 2 are in the same package I dont understand why I am getting the import error and if I'm getting the import error why am I getting the print out with what I would expect had I been able to import module2 what is adding to my confusion is that I have no issue with module3 when using

from package1.package2.module3 import module3
module3()

Thanks for any assistance



Read more here: https://stackoverflow.com/questions/64403661/importerror-when-importing-classes

Content Attribution

This content was originally published by Spooky at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: