3

Following structure:

app.py
  /package
    __init__.py
    foo.py
    bar.py

"foo.py" and "bar.py" contains both classes "Foo" and "Bar". Class "Foo" inherits from class "Bar". We have following code in the files...

"app.py":

from package import Foo

print Foo()

"__init__.py":

from foo import Foo
from bar import Bar

"foo.py":

class Foo(Bar):
    pass

"bar.py":

class Bar:
    pass

If I create an instance from "Foo" I became an name error "name 'Bar' is not defined". What I have to do to make it work? I am using Python 2.6.6 if it matters...

.oO(I'm new to Python)

2
  • 2
    you'll have to show real code, there isn't enough to go on here. Commented Feb 14, 2012 at 22:48
  • 1
    Put from bar import Bar to foo.py? Commented Feb 14, 2012 at 22:49

1 Answer 1

4

You need the line

from bar import Bar

in the file foo.py (not just in __init.py__).

Sign up to request clarification or add additional context in comments.

1 Comment

Don't use implicit relative imports. You could: from package import Bar (relies on the import in __init__.py) or from package.bar import Bar or from .bar import Bar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.