>>>>> ryles <ryle...@gmail.com> (r) wrote: >r> According to http://www.python.org/doc/essays/packages.html: >r> "The import statement first tests whether the item is defined in the >r> package; if not, it assumes it is a module and attempts to load it."
>r> However, I've noticed that once a module is imported using the >r> `from pkg import mod' syntax, if its name is deleted from the >r> package namespace then subsequent imports with `from' will fail. This is incorrectly stated. Also on the initial import it will fail, not just on subsequent imports. piet$ python Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from pkg import _impl _impl # this is from a print statement in _impl to show that it did import Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: cannot import name _impl >>> According to the documentation <http://docs.python.org/library/functions.html#__import__> the statement from pkg import _impl _temp = __import__('pkg', globals(), locals(), ['_impl'], -1) _impl = _temp._impl This fails because _temp (the imported module) doesn't have a binding for _impl because you deleted it. By the way, if you have a `normal' package that doesn't do anything with the name _impl, after the import the pkg module namespace will have got a binding for _impl although it is not in your code. It will have been put there by the import code. In the following example pkg2/__init__.py just contains the line A = 2 piet$ python Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from pkg2 import _impl _impl >>> import sys >>> dir(sys.modules['pkg2']) ['A', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '_impl'] >>> It is not clear to me, however what the order is of the statements in __init__.py and the insertion of _impl in the module namespace. -- Piet van Oostrum <p...@cs.uu.nl> URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: p...@vanoostrum.org -- http://mail.python.org/mailman/listinfo/python-list