memory overhead using from-import?

Erik Max Francis max at alcyone.com
Sun Dec 21 17:35:30 EST 2003


William Trenker wrote:

> After some experimenting with the /proc filesystem and a closer
> reading of the Python docs, I think I've answered my own question.  It
> looks like from-import does import the entire module.  So,
> from cgi import escape
> brings in the entire cgi module but only exposes the escape function
> to the importing module's namespace.

It makes very good sense that the from ... import ... syntax imports the
module.  Consider that the objects created in a module can be highly
interdependent on each other, not only for initial evaluation but also
containing links to one another.  When imported, a module runs in a
separate namespace, so it makes a lot of sense to divorce the importing
(and evaluation) of the module from how that module is exposed to other
modules (which objects are exposed in the new module to other modules
under what names).

The bigger question here is why you think you really should be avoiding
the full importation of the module.  In Python, modules are not simply a
list of exported names that are prebuilt in some way; they are in effect
subprograms which run when imported, and whose contents can be exposed
in various ways to an existing parent program (import X, import X as Y,
from X import Y).  The overhead involved in an interpreted language like
Python for importing the cgi module and only using one function inside
it vs. avoiding the importation at all is extremely minimal.

> The code, below, set me straight.

You didn't have to go that far.  Just look in sys.modules:

>>> import sys
>>> from math import sin
>>> math
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'math' is not defined
>>> sys.modules['math']
<module 'math' from '/usr/local/lib/python2.3/lib-dynload/math.so'>

-- 
 __ Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
/  \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ If it gets you there, then it's soul music.
    -- Sade Adu




More information about the Python-list mailing list