0

I have these modules and packages:

main.py/                          
        pkg1/                  
            __init__.py
            maclasse1.py

        pkg2/                  
           __init__.py
           maclasse2.py

main.py:

from pkg1 import maclasse1
from pkg2 import maclasse2
if __name__=="__main__":
   MC1=maclasse1.MaClasse1()
   MC1.ma_classe1()
   MC2=maclasse2.MaClasse2()
   MC2.ma_classe2()

maclasse1.py

from pkg2 import maclasse2
class MaClasse1:
   def ma_classe1(self):
       print "Ma Classe 1"
       c2=maclasse2.MaClasse2()
       c2.ma_classe2()

maclasse2.py

from pkg1 import maclasse1
class MaClasse2:
   def ma_classe2(self):
       print"Ma Classe 2"
       mc1=maclasse1.MaClasse1()
       mc1.ma_classe1()

When I run the main.py file, I get this error:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from pkg1 import maclasse1
  File "/home/nakkini/Desktop/tester/pkg1/maclasse1.py", line 1, in <module>
    from pkg2 import maclasse2
  File "/home/nakkini/Desktop/tester/pkg2/maclasse2.py", line 1, in <module>
    from pkg1 import maclasse1
ImportError: cannot import name maclasse1

How to resolve this problem ?

EDIT:

Following Tim's comment, I creat a tohelp.py in pkg3 that imports maclasse1.py whereas maclass2.py imports tohelp.py but I still have the same problem.

  • 8
    You have circular dependencies – Tim May 13 '15 at 14:56
  • @TimCastelijns can you calrify, please ? –  May 13 '15 at 14:57
  • 1
    File A imports file B, and file B imports file A – Tim May 13 '15 at 14:58
  • @TimCastelijns yes, but why is that a problem ? –  May 13 '15 at 14:58
  • 1
    This looks like **import recursion** – ZdaR May 13 '15 at 14:59
  • 3
    Short story: That's how importing system in Python works. Long story: http://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python – art-solopov May 13 '15 at 14:59
  • 1
    a needs b to load .. but b needs a ... so it can't load – Bastian May 13 '15 at 14:59
  • @Bastian but each one of them needs the other, so how to deal with this situation ? –  May 13 '15 at 15:00
  • Because file A need file B to be imported completely so it can load, but file B starts importing file A and for importing file A, file A must import file B first... – Mp0int May 13 '15 at 15:01
  • 1
    *how to deal with this situation ?* make a module that imports both files, and work from there – Tim May 13 '15 at 15:01
  • 1
    Sounds like both modules should be in the same package, if not both classes in the same module. Either that, or the common code that creates the mutual dependency should be refactored into a 3rd module. – chepner May 13 '15 at 15:04
  • No.. in tohelp.py include both maclasse1.py and maclasse2.py, and remove the imports in the maclasse files – Tim May 13 '15 at 15:57

1 Answers1

0

If you don't intend to change architecture, try this:

maclasse1.py

class MaClasse1:
    def ma_classe1(self):
        from pkg2 import maclasse2
        print "Ma Classe 1"
        c2=maclasse2.MaClasse2()
        c2.ma_classe2()

maclasse2.py

class MaClasse2:
    def ma_classe2(self):
        from pkg1 import maclasse1
        print"Ma Classe 2"
        mc1=maclasse1.MaClasse1()
        mc1.ma_classe1()
pavel_form
  • 1,760
  • 13
  • 14