詳解python關于多級包之間的引用問題
首先得明確包和模塊。
- 包:在一個目錄下存在__init__.py,那么該目錄就是一個包。
- 模塊:一個.py文件就是一個模塊。
我們可以通過from 包 import 模塊來引入python文件,也可以使用from 包.模塊 import 模塊中的函數(shù)或類。
具體看一下例子。
假設我們現(xiàn)在有以下的目錄:
我們想在main.py中使用package_a和package_b里面額模塊,可以這么使用:
from package_a import tmp2 from package_b import tmp4 tmp2.test_aTmp2() tmp4.test_bTmp4()
test_aTmp2()和test_bTmp4()是tmp2.py和tmp4.py里面的函數(shù)。
- 這里是package_a包中tmp2模塊下的test_aTmp2函數(shù)
- 這里是package_b包中tmp4模塊下的test_bTmp4函數(shù)
假設我們想在main.py中使用a_utils包下的aUtils模塊和b_utils包下的bUtils模塊,我們可以這樣:
from package_a import tmp2 from package_b import tmp4 from package_b.b_utils.bUtils import test_bUtils from package_a.a_utils import aUtils tmp2.test_aTmp2() tmp4.test_bTmp4() aUtils.test_aUtils() test_bUtils()
注意這里的兩種用法,
- 一種是我們直接通過:from 包.包.模塊 import 函數(shù)
- 另一種是from 包.包 import 模塊,并通過 模塊.函數(shù) 來使用相關的函數(shù)。
這里是package_a包中tmp2模塊下的test_aTmp2函數(shù)
這里是package_b包中tmp4模塊下的test_bTmp4函數(shù)
這里是package_a包中a_uitls包中的aUtils模塊下的test_aUitls函數(shù)
這里是package_b包中b_uitls包中的bUtils模塊下的test_buitls函數(shù)
下一個,假設我們想在tmp4.py使用tmp3.py中的函數(shù),因為在同一個包下,我們自然的是這么使用:
from tmp3 import test_bTmp3 test_bTmps3() def test_bTmp4(): print('這里是package_b包中tmp4模塊下的test_bTmp4函數(shù)')
我們在tmp4.py中繼續(xù)使用b_uitls包下的bUtils.py。
from tmp3 import test_bTmp3 from b_utils.bUtils import test_bUtils def test_bTmp4(): print('這里是package_b包中tmp4模塊下的test_bTmp4函數(shù)') # test_bUtils() test_bTmp3() test_bUtils()
這里是package_b包中tmp3模塊下的test_bTmp3函數(shù)
這里是package_b包中b_uitls包中的bUtils模塊下的test_buitls函數(shù)
現(xiàn)在的tmp4.py里面是以上那樣,假設我們現(xiàn)在重新運行之前的main.py:
from package_a import tmp2 from package_b import tmp4 from package_b.b_utils.bUtils import test_bUtils from package_a.a_utils import aUtils tmp2.test_aTmp2() tmp4.test_bTmp4() aUtils.test_aUtils() test_bUtils()
你會發(fā)現(xiàn)報錯了:
Traceback (most recent call last):
File "C:/Users/龔鷗波/Desktop/test_import/main.py", line 2, in <module>
from package_b import tmp4
File "C:\Users\龔鷗波\Desktop\test_import\package_b\tmp4.py", line 8, in <module>
from tmp3 import test_bTmp3
ModuleNotFoundError: No module named 'tmp3'
我們在main.py里面導入了tmp4,tmp4里面調(diào)用了tmp3,我門運行tmp4.py是沒問題,運行main.py有問題。這是因為,相對于main.py所在的目錄,tmp3所在是在包package_b下面,在tmp4中就不能直接導入,可以改寫成以下方式:
from .tmp3 import test_bTmp3 from .b_utils.bUtils import test_bUtils def test_bTmp4(): print('這里是package_b包中tmp4模塊下的test_bTmp4函數(shù)') test_bTmp3() test_bUtils()
這樣就不會報錯了:
這里是package_b包中tmp3模塊下的test_bTmp3函數(shù)
這里是package_b包中b_uitls包中的bUtils模塊下的test_buitls函數(shù)
這里是package_a包中tmp2模塊下的test_aTmp2函數(shù)
這里是package_b包中tmp4模塊下的test_bTmp4函數(shù)
這里是package_a包中a_uitls包中的aUtils模塊下的test_aUitls函數(shù)
這里是package_b包中b_uitls包中的bUtils模塊下的test_buitls函數(shù)
但是這時我們?nèi)ブ匦逻\行tmp4.py,發(fā)現(xiàn)會報錯了:
Traceback (most recent call last):
File "C:/Users/龔鷗波/Desktop/test_import/package_b/tmp4.py", line 8, in <module>
from .tmp3 import test_bTmp3
ModuleNotFoundError: No module named '__main__.tmp3'; '__main__' is not a package
這里.表示的是__main__,我暫時也不清楚這時是什么意思,不過我們可以這么改寫下:
try: from tmp3 import test_bTmp3 from b_utils.bUtils import test_bUtils except Exception as e: from .tmp3 import test_bTmp3 from .b_utils.bUtils import test_bUtils def test_bTmp4(): print('這里是package_b包中tmp4模塊下的test_bTmp4函數(shù)') test_bTmp3() test_bUtils()
這樣不論我們是運行main.py還是tmp4.py就都不會報錯了。
以上是自己在建項目中碰到的一個問題。
到此這篇關于詳解python關于多級包之間的引用問題的文章就介紹到這了,更多相關python 多級包引用問題內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Flink與Python進行實時數(shù)據(jù)處理的基本步驟
Apache Flink是一個流處理框架,用于實時處理和分析數(shù)據(jù)流,PyFlink是Apache Flink的Python API,它允許用戶使用Python語言來編寫Flink作業(yè),進行實時數(shù)據(jù)處理,以下是如何使用Flink與Python進行實時數(shù)據(jù)處理的基本步驟,需要的朋友可以參考下2024-09-09python函數(shù)參數(shù)*args**kwargs用法實例
python當函數(shù)的參數(shù)不確定時,可以使用*args和**kwargs。*args沒有key值,**kwargs有key值,下面看例子2013-12-12用Python監(jiān)控NASA TV直播畫面的實現(xiàn)步驟
本文分享一個名為"Spacestills"的開源程序,它可以用于查看 NASA TV 的直播畫面(靜止幀)2021-05-05Python實現(xiàn)刪除排序數(shù)組中重復項的兩種方法示例
這篇文章主要介紹了Python實現(xiàn)刪除排序數(shù)組中重復項的兩種方法,涉及Python數(shù)組元素的遍歷、判斷、刪除等相關操作技巧,需要的朋友可以參考下2019-01-01Django框架靜態(tài)文件使用/中間件/禁用ip功能實例詳解
這篇文章主要介紹了Django框架靜態(tài)文件使用/中間件/禁用ip功能,結(jié)合實例形式詳細分析了Django框架靜態(tài)文件的使用、中間件的原理、操作方法以及禁用ip功能相關實現(xiàn)技巧,需要的朋友可以參考下2019-07-07