I saw the following in a student's script, and to my surprise it works:
>>> import os.path
The question, in brief, is: How is this different from simple import os? Or is it the same, except for necessitating that os.path exists?
As you can see below, it doesn't define path in my scope but os.
>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'os']
So the question is: What does python do with this? For sure, it imports (and hence executes) the module os. It also ensures that os.path does resolve to something valid. (Thanks to @Lee and @Stuart for the links to the documentation that confirms it.) But is there more? Or is import pkg.submod always equivalent to import pkg (provided pkg.submod exists)?
If I import os, it already imports/executes any modules that os itself imports; so os.path is already loaded. Are there arrangements (not too exotic) where importing pkg.submod could lead to different code being executed, or in different order, or otherwise having different side effects, than just importing pkg? And are there legitimate use cases for the multi-segment form? (To put it differently: What's the point?)