0

I have two py files. a.py and b.py inside a package called test (has __init__.py)

and I want to access attribute self.items defined in the a.py below

import b

class Window(object):
    def __init__(self):
        self.items={'Magenta':'mag','Grey':'gre','Red':'red'}
    def getMats():
        newobj=b.BAR()
        selected = newobj.type_of_mats[1]

from a different py file b.py[below] so in b.py I imported the a module i.e.

import a

#now 

obj = a.Window()
print obj.items['Magenta']
class BAR(object):
    def myMat(self):
        type_of_mats=['ground', 'corridor', 'Outdoor']

should'nt the above prints mag since or how else I should do it ?

Andrei
  • 55,890
  • 9
  • 87
  • 108
Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197

3 Answers3

1

see these stackoverflow questions on circular imports (a) and (b). I think it depends on the compiler / interpreter being used. In my case your code does not give me recursion depth exceeded, but this does

Hope it helps.

Community
  • 1
  • 1
pranshus
  • 187
  • 6
0

Circular import. a.py imports b and b.py imports a again. Chicken/egg problem! And python cannot solve that for you, so it warns you about a maximum recursion depth (it keeps on jumping from a to b to a to b to a...).

You'll have to fix it so that the import is one-way only.

Reinout van Rees
  • 13,486
  • 2
  • 36
  • 68
0

You have a circular dependency: a is importing b, which is in turn importing a. Since those two imports are at module level, they can never complete.

In your case, you could fix it by moving the import b into the getMats method.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895