0

I have 2 class in python cl1 in f1.py file and cl2 in f2.py file. I wrote import f2

import f2

class cl1:
  a = f2.cl2()

But i see error in a = f2.cl2(): module object has no attribute 'cl2'

Why?

Thank you.

2
  • 5
    could you please post the contents of f2.py? I have to assume the error message means exactly that: You have not defined cl2 in f2... Commented Feb 26, 2010 at 10:35
  • @sterh: Please UPDATE the question to post the relevant code from f2.py. Commented Feb 26, 2010 at 12:21

2 Answers 2

1

sorry, i was wrong: your problem is probably that you have a circular import: f1 imports f2 and vice versa. check your design, as it usually should be possible to design your software without a circular import.

see: this

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

2 Comments

I deleted import f1 in f2.py and now i have Import error in f1: cannot import name cl2
if f1.py still contains an "import f2" then your code should work fine. f2.py doesn't have to know anything about the modules of f1.py
0

The following code works just fine (if you're using Python 3 you can omit the (object) parts, but in Python 2 you should leave them in -- they're not responsible for your bug, but if you get used to omitting them you'll have strange problems in the future as your code grows...):

f2.py is:

class cl2(object):
  pass

f1.py is:

import f2

class cl1(object):
  a = f2.cl2()

If your code is not working, it must be different from this. Please confirm that this simple code is working for you, then show us (by editing your original question, not by posting comments or "answers") how your non-working code differs (lack of imports, circular imports, wrong imports, or whatever else).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.