0

My code is a bit complex so i'll show an example:

file A.py:

from B import B

class A():

def __init__(self):

    self.id = 5

    self.b = B()

file B.py

from A import A

class B():

def __init__(self):

    self.id = 15

    self.a = A()

This is the exception i get:

 Traceback (most recent call last):
 File "F:/Project/Code/A.py", line 1, in <module>
 from B import B
 File "F:\Project\Code\B.py", line 1, in <module>
 from A import A
 File "F:\Project\Code\A.py", line 1, in <module>
 from B import B
 ImportError: cannot import name B

All I want is A to contain an instance of B and B to contain an instance of A. I know that all I have to do is to convert them to a single file but I don't want to, my code is on much larger scales and my teacher force me to keep multiple short scripts instead of one long code.

Any suggestion will be appreciated.

Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
Maor2871
  • 147
  • 1
  • 13
  • 1
    "All I want is A to contain an instance of B and B to contain an instance of A" - even if you resolve the circular import problem, this is still a fundamentally broken design. An A contains a B, which contains another A, which contains another B, etc. Creating one of these objects requires you to create an infinite number of objects. – user2357112 Feb 02 '17 at 21:47
  • 1
    See this question: http://stackoverflow.com/questions/11698530/two-python-modules-require-each-others-contents-can-that-work – ScottSmudger Feb 02 '17 at 21:48
  • I suppose he could specify a maximum creation recursion depth somehow but that's a pretty heavy meta topic – Woody1193 Feb 02 '17 at 22:09
  • Closed as a dupe that addresses the literal question re: the ImportError. As for being able to set up cross-references at objection creation time in a way that doesn't result in infinite recursion (assuming that it's acceptable to reuse instances of `A` and `B` for the contained objects' members), that calls for a separate question. – Charles Duffy Feb 03 '17 at 01:31

3 Answers3

1

What you have is called an import-loop. How you go about fixing is moving the mutually dependent code to a class C that both then reference.

In C.py:

def class C:

    def __init__(self):
        # Do some stuff

    # other methods

In A.py:

 from C import C

 def class A(C):

     def __init__(self):
        # Do some stuff in A

     # other methods

In B.py:

from C import C

def class B(C):

    def __init__(self):
        # Do some stuff in B

    # other methods
Woody1193
  • 7,252
  • 5
  • 40
  • 90
0

You are getting the error because your scripts have recursive imports.

A.py depends from B module. But B.py depends from A module. And Python can't to resolve imports.

You can write all classes in one module. Or make sure that there was no cyclic dependency.

The same problem can be with import of variables, functions.

Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
0

You can not have a two-way dependency, because that would mean every b property in A is a B instance that contains its own A, and so on.

A
- id: 5
- b: B
     - id: 15
     - a: A
          - id: 15
          - b: B ...
Uriel
  • 15,579
  • 6
  • 25
  • 46