import a.b also imports a?

后端 未结 4 1281
故里飘歌
故里飘歌 2021-02-20 03:26

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; pr         


        
相关标签:
4条回答
  • 2021-02-20 03:57

    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.

    0 讨论(0)
  • 2021-02-20 04:08

    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).

    0 讨论(0)
  • 2021-02-20 04:11

    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.

    0 讨论(0)
  • 2021-02-20 04:14

    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.

    0 讨论(0)
提交回复
热议问题