[Python-Dev] Relative import

Skip Montanaro skip at pobox.com
Thu Dec 18 09:49:39 EST 2003


    >> It has been proposed (more than once I think) that such a stdlib
    >> package could be created in parallel using symlinks....

    Michael> Are you sure that you won't get multiple copies of the same
    Michael> module floating around?  E.g. will you have

    Michael> from httplib import HTTPConnection as HC1
    Michael> from std.httplib import HTTPConnection as HC2
    Michael> HC1 is HC2

    Michael> I think in the scheme sketched above this will be false, which
    Michael> kills the idea stone dead.

Maybe the symlink idea won't work.  It seems that it would work to import
the standard modules from std/__init__.py though.  I performed a simple
experiment.  I created the std directory, added a symlink in it for the
random module and added an __init__.py file with this content:

    import sys
    import urlparse

Here's the result:

    >>> import random
    >>> from std import random as random2
    >>> random is random2
    False
    >>> import urlparse
    >>> from std import urlparse as urlparse2
    >>> urlparse is urlparse2
    True
    >>> import sys
    >>> from std import sys as sys2
    >>> sys is sys2
    True

If we added a significant number of modules to std/__init__.py, startup
would slow to a crawl.  I imagine some sort of delayed import mechanism
could be crafted to avoid this problem though.

Skip



More information about the Python-Dev mailing list