5

In our projects we have level 'controls' with the following modules: 'grid', 'gridcell', 'combo' and etc. Grid module imports gridcell module since grid consists of cells, while any cell can contain combo inside it. So initially we starting to use 'from ... import ...' statements inside these classes the following way:

#grid.py
from controls.gridcell import cell
#gridcell.py
from controls.combo import combo

But this was ok until we start using grid as a combo content. As soon as we start doing this we were required to add 'from grid import grid' statement into the 'combo.py'. After doing this we get an import exception:

from controls.gridcell import gridcell 
ImportError: Cannot import name gridcell 

EDITED:

I have tried also 'import ... as ...' and get the following error:

import controls.gridcell as gridcell
AttributeError: 'module' object has no attribute 'gridcell'

I have read several articles and all that i have found about how to solve this is to use 'import' statement without from, e.g:

#grid.py
import controls.gridcell
#gridcell.py
import controls.combo
#combo.py
import controls.grid

But this leads us to use full names like 'controls.gridcell.cell', 'controls.combo.combo', 'controls.grid.grid' and so on.

So my question is - Is there any other way how to do this(so it would be available to use shorter names) or this is the only way of how to resolve this issue?

Sorry if i miss something

Thanx to all

Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52

2 Answers2

9
import controls.gridcell as gridcell

etc. etc. etc. etc.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 3
    Better yet, for this particular case: `from controls import gridcell`. 'import as' is mostly useful when you want a *different* name for the local name. – Thomas Wouters Jun 16 '11 at 16:50
  • @Thomas: I think the OP ran into the problem described in [this question I asked a few days back](http://stackoverflow.com/questions/6351805/cyclic-module-dependencies-and-relative-imports-in-python). When you have cyclic module depedencies, only `import ...` works, while `from ... import ...` doesn't. – Sven Marnach Jun 16 '11 at 16:53
  • Wow it works, thank you, I have tried this previously but due to unknown reason for me it was not working in my previous console, but it works well on 2.7. Is there any way of how to do the same using the following syntax 'gridcell = __import__('controls.gridcell'))' or it not acceptable? – Artsiom Rudzenka Jun 16 '11 at 16:54
  • Modules within packages are not created in the same manner as global names, so I don't believe that `from ... import ...` will be a problem. I just really like `as`. – Ignacio Vazquez-Abrams Jun 16 '11 at 16:55
  • @Atrsiom: `__import__()` returns the top-level package; you will have to access the `gridcell` attribute to get the module. – Ignacio Vazquez-Abrams Jun 16 '11 at 16:55
  • Indeed, `from controls import gridcell` and `import controls.gridcell as gridcell` both work (and fail) in the same situations (and are almost identical in how they're implemented.) – Thomas Wouters Jun 16 '11 at 16:57
  • @Ignacio, oh, yes - your truth, thank you, just has rechecked it. – Artsiom Rudzenka Jun 16 '11 at 16:58
  • I have tried both ways on my real project and both of them throwing exceptions. I edited my question to provide more details on this. – Artsiom Rudzenka Jun 23 '11 at 06:11
4

you can also move the imports into the functions.

def foo():
    from controls.gridcell import cell
    from controls.combo import combo

if you have an init() function this can be convenient.

Guy
  • 14,178
  • 27
  • 67
  • 88