[Python-Dev] Re: [Import-sig] Re: Proposal for a modified import mechanism.

Fredrik Lundh fredrik@pythonware.com
Mon, 12 Nov 2001 18:05:30 +0100

James C. Ahlstrom wrote:
> > Currently, os.py in a package masks the real one from
> > anywhere inside the package. This would extend that to
> What???
> When Python starts, it imports site.py which imports os.py.
> So os.py gets loaded, and won't normally get re-loaded.

a python system doesn't necessarily load site.py, but

> So if os.py is in package1, it won't get loaded for "import os",
> but it would get loaded for "import package1.os".  Are you saying
> that "import package1.package2.os" will load package1/os.py?

no, but an "import os" from inside the "spam.py" module installed
as "package1.spam" will attempt to import "package1.os" before
it looks for "os" in the path.

consider this (somewhat simplified) directory structure:


and this interpreter session:

$ python -vvS
Python 2.1.1
Type "copyright", "credits" or "license" for more information.
>>> import package1
import package1 # directory pythonware
# trying package1/__init__.py
# package1/__init__.pyc matches package1/__init__.py
import pythonware # precompiled from package1/__init__.pyc
>>> import package1.spam
# trying package1/spam.py
# package1/spam.pyc matches package1/spam.py
import pythonware.spam # precompiled from package1/spam.pyc
# trying package1/os.py
# package1/os.pyc matches package1/os.py
import pythonware.os # precompiled from package1/os.pyc

(note the last line)

> I hope that "import os" will not load package1/os.py, will it?

depends on who's calling it.