cjm2671
cjm2671

Reputation: 19456

Python inheritance - name is not defined

I've got the following code (shortened for clarity):

face.py:

from frame import *

class Face(Frame):
    def hello(self):
        print 'hello'

frame.py:

from face import *

class Frame(object):
  def __init__(self, image):
    self.image = image

I'm getting the following error:

Traceback (most recent call last):
  File "2.py", line 2, in <module>
    from frame import *
  File "/home/code/iris/frame.py", line 4, in <module>
    from face import *
  File "/home/code/iris/face.py", line 7, in <module>
    class Face(frame.Frame):
NameError: name 'frame' is not defined

which I think is to do with the way I've either:

Any ideas what I've done wrong? Also, if anyone can explain where 'import' is necessary that would be helpful!

Thanks! Chris.

Upvotes: 2

Views: 5091

Answers (1)

Ankur Aggarwal
Ankur Aggarwal

Reputation: 3101

You are entering into the trap of circular dependency. Your face class is depending on the frame class and your frame class is depending on face class thus creating a circular deadlock like situation. Taking reference from Handle circular dependencies in Python modules?

There are ways around this problem in python: The good option: Refactor your code not to use circular imports. The bad option: Move one of your import statements to a different scope.

Upvotes: 3

Related Questions