• Python 包内的导入问题(绝对导入和相对导入)


    基本概念

    Python 中的包,即包含 __init__.py 文件的文件夹。

    对于 Python 的包内导入,即包内模块导入包内模块,存在绝对导入和相对导入问题。

    普通 Python 模块的搜索路径

    1. 在当前模块所在路径中搜索导入模块

    2. 在环境变量 PYTHONPATH 指定的路径列表中搜索导入模块

    3. 在 sys.path 指定的路径列表中搜索导入模块

    Python import 的步骤

     Python 所有加载的模块信息都存放在 sys.modules 字典结构中,当 import 一个模块时,会按如下步骤来进行

    1. 如果 import A,检查 sys.modules 中是否已经有 A,如果有则不加载,如果没有则为 A 创建 module 对象,并加载 A,即可以重复导入,但只加载一次。
    2. 如果 from A import B,先为 A 创建 module 对象,再解析 A,从中寻找 B 并填充到 A 的 __dict__ 中。

    相对导入与绝对导入

    绝对导入的格式为 import A.B 或 from A import B,相对导入格式为 from .A import B 或 from ..X import Y,. 代表当前模块,.. 代表上层模块,... 代表上上层模块,依次类推。

    相对导入对于包的维护优势

    相对导入可以避免硬编码带来的包维护问题,例如我们改了某一层包的名称,那么其它模块对于其子包的所有绝对导入就不能用了,但是采用相对导入语句的模块,就会避免这个问题。

    需要注意:存在相对导入语句的模块,是不能直接运行的。 例如,对于如下层次结构的 Digital.py 文件

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    ##############################################################################
    # Purpose: to demo underlayer import upperlayer.
    ##############################################################################
    #
    #      PHONE
    #      │  common_util.py   -> setup()
    #      │  __init__.py
    #
    #      ├─Fax
    #      │      G3.py        -> bar()
    #      │      __init__.py
    #
    #      ├─Mobile
    #      │      Analog.py    -> foo()
    #Digital.py
    #      │      __init__.py
    #
    #      ├─Pager
    #      │      Page.py
    #      │      __init__.py
    #
    #      └─Voice
    #              Isdn.py
    #              __init__.py
    #
    ##############################################################################
    
    from .Analog import foo          # ValueError: Attempted relative import in non-package
    from ..common_util import setup  # ValueError: Attempted relative import in non-package
    from ..Fax.G3 import bar         # ValueError: Attempted relative import in non-package
    
    if __name__ == '__main__':
    
        foo()
        setup()
        bar()

    如果上述代码直接运行,将导致 ValueError 异常,

    ValueError: Attempted relative import in non-package

    这是因为:一个模块直接运行,Python 认为这个模块就是顶层模块,不存在层次结构,所以找不到其它的相对路径。

    而要正确运行,就要显式的指定路径,如下,

    C:workspaceX_python>python -m Phone.Mobile.Digital
    This is foo() from Phone.Mobile.Analog
    This is setup() from Phone.common_util
    This is bar() from Phone.Fax.G3

    当然,我们一般不会直接运行包内的某个模块,这里只是做个说明。

    绝对导入对于包维护的劣势

    例如,对于如下层次结构的 Digital.py 文件,

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    ##############################################################################
    # Purpose: to demo underlayer import upperlayer.
    ##############################################################################
    #
    #      PHONE
    #      │  common_util.py   -> setup()
    #      │  __init__.py
    #
    #      ├─Fax
    #      │      G3.py        -> bar()
    #      │      __init__.py
    #
    #      ├─Mobile
    #      │      Analog.py    -> foo()
    #Digital.py
    #      │      __init__.py
    #
    #      ├─Pager
    #      │      Page.py
    #      │      __init__.py
    #
    #      └─Voice
    #              Isdn.py
    #              __init__.py
    #
    ##############################################################################
    
    # from .Analog import foo          # ValueError: Attempted relative import in non-package
    # from ..common_util import setup  # ValueError: Attempted relative import in non-package
    # from ..Fax.G3 import bar         # ValueError: Attempted relative import in non-package
    
    from Phone.Mobile.Analog import foo
    from Phone.common_util import setup
    from Phone.Fax.G3 import bar
    
    if __name__ == '__main__':
    
        foo()
        setup()
        bar()

    上述代码可以直接运行。
    但是,绝对导入的硬编码模式,如果在包中存在很多 Digital.py 类似模块,都采用了 from Phone.common_util import setup 的语句,如果有一天要更改 common_util 包(文件夹)的名字,那么会影响所有相关的代码。而采用相对导入就没有这个问题。

    不过,绝对导入更清晰,如果包不是特别复杂,不是特别易变,那么还是建议采用绝对导入。(个人观点,仅供参考)

    再举一个包内导入的例子,目录结构为,

    #   myabc/
    #   ├── abc.py
    #   ├── __init__.py
    #   └── xyz.py
    
    # abc.py
    
    def foo():
        print("This is foo from local abc module!")
    
    # xyz.py
    
    ##########################################
    #import .abc                  # invalid (due to abc is not a package, so cannot import directly)
    #import . abc                 # invalid (reason as above)
    ##########################################
    
    #from .abc import foo          # valid
    from . abc import foo          # valid
    
    def bar():
        print('bar - ', end='')
        foo()

    外部使用 myabc 包,

    >>> import myabc.xyz
    >>> myabc.xyz.bar()
    bar - This is foo from local abc module!
    >>> 
    >>> from myabc import xyz
    >>> xyz.bar()
    bar - This is foo from local abc module!
    >>> 
    >>> 
    >>> import myabc.abc
    >>> myabc.abc.foo()
    This is foo from local abc module!
    >>> 
    >>> from myabc import abc
    >>> abc.foo()
    This is foo from local abc module!

    再举个例子, 

    #    myfact/
    #    ├── factory.py
    #    ├── __init__.py
    #    └── xyz.py
    
    # factory.py 
    def foo():
        print("This is foo from local factory module!")
    # xyz.py
    #from myfact import factory # Valid, absolute #from myfact.factory import foo # Valid, absolute

    #from factory import foo # Invalid! ModuleNotFoundError: No module named 'factory' #from .factory import foo # Valud, relative from . factory import foo # Valud, relative
    def bar(): 
    print('bar - ', end='')
    foo()

    外部使用 myfact 包,

    >>> import myfact.xyz
    >>> 
    >>> myfact.xyz.bar()
    bar - This is foo from local factory module!

    (完)

  • 相关阅读:
    SciPy
    时间序列
    bytes 与 str 转换
    tensorflow
    Python3+Cuda+Cudnn+GPU
    TensorFlow models
    saltstack
    docker
    分布式文件系统
    创建RHCS集群环境 创建高可用Apache服务
  • 原文地址:https://www.cnblogs.com/gaowengang/p/8543840.html
Copyright © 2020-2023  润新知