">

(original) (raw)




On Mon, Apr 1, 2013 at 4:20 PM, Kristján Valur Jónsson <kristjan@ccpgames.com> wrote:

I just ran into the issue described in http://stackoverflow.com/questions/6351805/cyclic-module-dependencies-and-relative-imports-in-python.

This is unfortunate, because we have been trying to move towards relative imports in order to aid flexibility in package and library design.

The relative import syntax

(from foo import bar) is a getattr type of lookup (i.e. import foo, then get attr from it).

This is in contrast with absolute import

import foo.bar (get the module foo.bar from sys.modules or import it)

bar = foo.bar


Or \`\`import foo.bar as bar\`\`

the latter works with partially initialized modules, but not the former, rendering two sibling modules unable to import each other using the relative syntax.


Clarification on terminology: the \`\`from .. import\`\` syntax is in no way relative. Relative imports use leading dots to specify relative offsets from your current position (i.e. as Barry said). It's more of a syntax for facilitating binding long names (e.g. foo.bar) to shorter names (bar). It's just unfortunate that it can lead to circular import issues when people start pulling in objects off of modules instead of modules alone.

as far as I know, relative imports are only supported using the former (import from) syntax. Are there any plans to alleviate this by allowing proper relative imports? After all, relative imports and packages go hand in hand.


No, there are no plans to either tweak \`\`from ... import\`\` statements nor introduce a new syntax to deal help alleviate circular imports.

-Brett

K


_______________________________________________

Python-Dev mailing list

Python-Dev@python.org

http://mail.python.org/mailman/listinfo/python-dev

Unsubscribe: http://mail.python.org/mailman/options/python-dev/brett%40python.org