import a.b also imports a?

2019-04-20 08:02发布

问题:

When I import a subpackage in a package, can I rely on the fact that the parent package is also imported ?

e.g. this works

python -c "import os.path; print os.getcwd()"

Shouldn't I explicitly import os for os.getcwd to be available ?

回答1:

There's an important thing to know about packages, that is that there is a difference between being loaded and being available.

With import a you load module a (which can be a package) and make it available under the name a.

With from a import b you load module a (which definitely is a package), then load module a.b and make only this available under the name b. Notice that a also got loaded in the process, so any initialization this is supposed to do will have happened.

With import a.b you load both and make both available (under the names a and a.b).



回答2:

This is a good question. If you look at the source code for os.py you find this line:

sys.modules['os.path'] = path

So there's our module. But what's path? Well that depends on your OS. For Windows, it's defined in this block:

elif 'nt' in _names:
    name = 'nt'
    linesep = '\r\n'
    from nt import *
    try:
        from nt import _exit
    except ImportError:
        pass
    import ntpath as path

    import nt
    __all__.extend(_get_exports_list(nt))
    del nt

Basically, os.path is special in this context.

Short version: Python does some stuff behind the scenes to make os.path. You probably shouldn't rely on it to get other modules. "Explicit is better than implicit" is how the zen goes.



回答3:

It works and it is reliable. What happens under the hood is when you do

import os.path

then os gets imported and then os.path.



回答4:

Yes, you can rely on it always working. Python has to include os in the namespace for os.path to work.

What won't work is using the from os import path notation. In that case, the os module is not brought into the namespace, only path.