1. 程式人生 > >Python 包內的匯入問題(絕對匯入和相對匯入)

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:\workspace\X_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 包(資料夾)的名字,那麼會影響所有相關的程式碼。而採用相對匯入就沒有這個問題。

不過,絕對匯入更清晰,如果包不是特別複雜,不是特別易變,那麼還是建議採用絕對匯入。