0
votes

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

1
Why is the name module1 in the file file module2 isn't that the error ? - azro
Thanks @azro had just spotted that - Spooky
@azro I made a typo in Module2.py I should have called the class module2 instead of module1, thanks for spotting it - Spooky

1 Answers

0
votes

I spotted the issue, I made a typo in Module2.py as

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

it should have been

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