Package importing question

Daniel Dittmar daniel.dittmar at sap.com
Wed Mar 10 04:42:12 EST 2004


Nick Patavalis wrote:
> So the first obvious question is how was m0 able to find the p0
> dictionary and the binding to m1? Am I getting something totally
> wrong so far?

m0 knows that it belongs to package p0 (possibly through the __file__ or the
__name__ variable). When 'import m1' is executed, the python import
mechanism
- locates the package module p0 in sys.modules
- searches directory p0 for m1.py
- loads m1.py and creates a module object
x inserts the newly created module object into the namespace of p0, the name
will always be m1
- inserts the newly created module object into the namespace of m0, the name
can be specified through import ... as ...

Line x happens so that a future import p0.m1 will find the same module
object.

You don't see sys in dir (p0) because the namespace of m0 isn't copied into
p0. Try it with
import m1 as mtest
You should see mtest in dir (m0) and m1 in dir (p0), but both being the same
module object.

Daniel






More information about the Python-list mailing list