On Sunday 17 July 2005 12:47 pm, kimes wrote: > Thanks for all you guys help.. > I'm still confused about that.. > When I just call import os.path without calling import os.. > In that case, you mean 'import os' is called implicitly? > Why? and How? > > how python knows it should call import when we call import os? > Please make me clear.. :)
All you have to do is import os Python "knows" to import "os.path" because "os" tells it to. That is to say, somewhere in "os" is code (conceptually) like: import path The os.path module is a sub-module of os. What makes it particularly interesting is only that it is not always the *same* module -- the particular os.path module that is loaded is determined by your architecture. On a POSIX system (including Unix and Linux) it will actually be an alias for "posixpath", but it will be something else on Windows and Macintosh systems. OTOH, the same thing will happen if you say: import os.path since it must import "os" in order to get to "path". I don't think that Python is guaranteed to load the entire contents of such dotted imports, although it appears to do so for the case of "os" (I believe it depends on the code in the module). Occasionally you will find packages that require you to load subpackages explicitly: import mypackage import mypackage.subpackage but not with "os". Oh, and what Peter Otten meant by "module, or package, it doesn't matter here" is that the two terms are roughly equivalent, although there is an internal distinction -- a "module" is a single Python source file, whereas a "package" is a directory of source files which are set up to load like a single module. For the purposes of this discussion, the two terms are basically interchangeable (we really ought to have a single term that can mean either -- I usually speak loosely of "modules" regardless of which way they are actually defined). -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com -- http://mail.python.org/mailman/listinfo/python-list