[Python-Dev] Proposal: Slightly Changed Semantics for from-Import

Armin Ronacher armin.ronacher at active-4.com
Sat Mar 22 12:53:14 CET 2008


Hi all,


I propose a small change on the "from"-import behavior.  Currently there are two
ways to import a module name foo from bar.  Namely "import foo.bar as bar" and
"from foo import bar".  The main problem with the second is that it will not
work in every situation.

Modules are registered with their names in sys.modules before the import but
become and attribute of their parent after.  This leads to the surprising
behavior that "from foo.bar import baz" works but "from foo import bar" not
until the foo.bar module finished setting up.

This behavior is especially surprising if you have circular dependencies and you
notice that your imports work properly if you import a specific module before
another one and will fail if you import it afterwards although it doesn't seem
like that should make any difference.

My proposal is that "from foo import bar" returns sys.modules["foo.bar"] if this
one exists and the foo.bar attribute lookup failed.

If a test case is needed I can attach one later on.


Regards,
Armin



More information about the Python-Dev mailing list