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,. 代表當前模組,.. 代表上層模組,... 代表上上層模組,依次類推。

需要注意:存在相對匯入語句的模組,是不能直接執行的。 # │ __init__.py # # ├─Fax # │ G3.py -> bar() # │ __init__.py # # ├─Mobile # │ Analog.py -> foo() #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

當然,我們一般不會直接執行包內的某個模組,這裡只是做個說明。

# │ __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!