關於Python如何避免迴圈匯入方法介紹

來源:互聯網
上載者:User
在大型的Python工程中,由於架構設計不當,可能會出現模組間相互引用的情況。下面這篇文章主要給大家介紹了關於如何避免Python的迴圈匯入問題的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

Python 中使用package時,出現迴圈匯入問題十分常見,我們建立如下package來說明這個問題:


pkg ├── __init__.py ├── module_a.py └── module_b.py

其中,

__init__.py 將pkg指定為一個Python package

module_a.py中定義了一個action_a()函數,該函數引用了module_b.py中的一個attribute,如一個函數或變數

module_b.py中定義了一個action_b()函數,該函數引用了module_a.py中的一個attribute,如一個函數或變數

這種情況下,執行該package時會拋出circular import error錯誤,即循環參考,因為module_a試圖去引入module_b時,而module_b首先要引入module_a,這會導致Python解譯器無法執行下去。

然而,我們可以通過一些巧妙的方法,讓上面的邏輯正常工作,同時避免迴圈引入的錯誤。

那麼,什麼時候它能正常工作,什麼時候不能正常工作,而那些能夠正常工作的情況又是什麼原因呢?

何時它能正常工作?

1. 在module頂部引入,不要用from,相對引入,只在Python 2中有效

在module的頂部import,如import another_module,module 中的函數以another_module.attribute的方式引用another_module中的函數或變數等。這種方式之所以有效,是由於import another_module是基於目前的目錄的相對參照,而且是一種隱式引用,如果從另一個package中引入module時,就可以失效了。另外,import another_module這種文法在Python3 中已經不支援了,所以不要在代碼中用這種方法來避免迴圈引入。

如:


# pkg/module_a.py from __future__ import print_functionimport module_b def action_a(): print(module_b.action_b.__name__)  # pkg/module_b.pyfrom __future__ import print_functionimport module_a def action_b(): print(module_a.action_a.__name__)

2. 在module的頂部引入,不要用from,絕對引入

在module的頂部import,使用從package開始的絕對路徑,如import package.another_module,module 中的函數以package.another_module.attribute的方式引用another_module中的函數或變數等。之所以要掛上package name來引入,是由於import .another_module這種形式的“相對引入”會報語法錯誤,而掛上package的絕對引入,Python 2和3都支援

案例:


# pkg/module_a.pyfrom __future__ import print_functionimport pkg2.module_b def action_a(): print(pkg2.module_b.action_b.__name__)  # pkg/module_b.pyfrom __future__ import print_functionimport pkg2.module_a def action_b(): print(pkg2.module_a.action_a.__name__)

3. 在module底部引入another module的attribute,而非another module,用from

在module的底部import(至少要在被引用的attribute之後import),直接引入another module的attribute,如from package.another_module import attribute,相對引入也支援,如from .another_module import attribute,module中的函數直接使用被引用的attribute即可。

如:


# pkg/module_a.pyfrom __future__ import print_function def action_a(): print(action_b.__name__) from .module_b import action_b  # pkg/module_b.pyfrom __future__ import print_function def action_b(): print(action_a.__name__) from .module_a import action_a

4. 函數頂部引入,可以用from

在module的function頂部import,如from package import another_module,也支援相對引入,引入module或attribute均可。

如:


# pkg/module_a.pyfrom __future__ import print_function def action_a(): from . import module_b print(module_b.action_b.__name__)  # pkg/module_b.pyfrom __future__ import print_function def action_b(): from . import module_a print(module_a.action_a.__name__)


# pkg/module_a.pyfrom __future__ import print_function def action_a(): from .module_b import action_b print(action_b.__name__)  # pkg/module_b.pyfrom __future__ import print_functiondef action_b(): from .module_a import action_a print(action_a.__name__)

這種方式雖然Python 2和3都支援,但編碼不夠優雅,影響代碼可讀性,不建議使用


本文討論的問題,是Python中調用package時,應如何避免迴圈引入

當直接在命令列執行一個Python module時,適用情況不完全相同

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.