os.path如何映射到posixpath.pyc而不是os/path.py?

Rei*_*ica 3 python import alias module path

Python中处理这种"别名"的底层机制是什么?

>>> import os.path
>>> os.path.__file__
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/posixpath.pyc'
Run Code Online (Sandbox Code Playgroud)

Bas*_*ard 6

取自CPython 2.6上的os.py:

sys.modules['os.path'] = path
from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
    devnull)
Run Code Online (Sandbox Code Playgroud)

path 之前定义为特定于平台的模块:

if 'posix' in _names:
    name = 'posix'
    linesep = '\n'
    from posix import *
    try:
        from posix import _exit
    except ImportError:
        pass
    import posixpath as path

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

elif 'nt' in _names:
# ...
Run Code Online (Sandbox Code Playgroud)