Imports and cyclical dependencies

Brendan Abel 007brendan at gmail.com
Wed Nov 13 22:20:58 EST 2013


I have a relatively large python package that has several cyclical dependencies.  The cyclical dependencies typically aren't a problem so long as I'm just importing modules, and not named attributes (ie. function, class, globals), which may not be defined at a given point in the import routine because of the cyclical dependency.

The problem I'm encountering is that relative imports and any import that uses the  "from package import module" or "import package.module as module" syntax results in an ImportError (or AttributeError for the "as" syntax).  I would much rather use this import syntax for several reasons:

1. Relative imports seem to be the recommended syntax going forward for intra-package imports.
2. One of the two import syntaxes that work does not work in Python3, or if absolute imports are used in Python2.
3. Imports with nested subpackages are wordy (eg. package.packa.packb.module) and make the code longer and harder to read.

I posted the question to stackoverflow with an example and got some useful information, but not the answer I was looking for.

http://stackoverflow.com/questions/19822281/why-do-these-python-import-statements-behave-differently

At this point, I'm wondering if I should report this as a bug/feature request, since it seems that these imports should be able to resolve to the correct module.



More information about the Python-list mailing list