On 11/02/2012 12:11 PM, Michael Schwarz wrote:
… which doesn't work. Some of the modules reference other modules in
the same package. I'm not talking about cyclic references, but, for
example, the "dialog" module uses the "transaction" module. The
problem is that the "dialog" module uses the same mechanism shown
above to import the other modules from it's package. This means that
modules and packages are imported in this order:
- Application code executes "from the_library import sip"
- the_library/__init__.py is executed. No imports here.
- the_library/sip/__init__.py executes "from . import [...], dialog"
- the_library/sip/dialog.py executes "from the_library import sip"
In a way, you do have a cyclic reference. If you think of "import sip"
as "from sip import __init__ as sip", it should become apparent.
Anyway, I see two ways around this. One is to rename the package and
create a module under the_library with the old package name that imports
what you want. To keep using the same names, every module in the package
can import siblings like so (where sippkg is the new package name):
import the_library.sippkg.transaction
sip = the_library.sippkg
The second way is to not use import at all to load sibling modules
(except in __init__.py), and instead use:
sip = sys.modules['the_library.sip']
This will work as long as you are careful to load the modules from
__init__.py in the correct order, where each module's dependencies must
appear before the module, in the import list.
--
http://mail.python.org/mailman/listinfo/python-list