tags:

views:

86

answers:

2

Hi to all. 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.

+1  A: 

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

ptikobj
I deleted import f1 in f2.py and now i have Import error in f1: cannot import name cl2
shk
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
ptikobj
A: 

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).

Alex Martelli