濮阳杆衣贸易有限公司

主頁 > 知識庫 > 詳解python關(guān)于多級包之間的引用問題

詳解python關(guān)于多級包之間的引用問題

熱門標(biāo)簽:ai電銷機(jī)器人源碼 外呼并發(fā)線路 地圖標(biāo)注審核表 百度地圖標(biāo)注沒有了 湛江智能外呼系統(tǒng)廠家 ai電話機(jī)器人哪里好 西藏房產(chǎn)智能外呼系統(tǒng)要多少錢 宿遷星美防封電銷卡 長沙高頻外呼系統(tǒng)原理是什么

首先得明確包和模塊。

  • 包:在一個目錄下存在__init__.py,那么該目錄就是一個包。
  • 模塊:一個.py文件就是一個模塊。

我們可以通過from 包 import 模塊來引入python文件,也可以使用from 包.模塊 import 模塊中的函數(shù)或類。

具體看一下例子。
假設(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ù)

假設(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ù) 來使用相關(guān)的函數(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ù)

下一個,假設(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里面是以上那樣,假設(shè)我們現(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里面導(dǎo)入了tmp4,tmp4里面調(diào)用了tmp3,我門運行tmp4.py是沒問題,運行main.py有問題。這是因為,相對于main.py所在的目錄,tmp3所在是在包package_b下面,在tmp4中就不能直接導(dǎo)入,可以改寫成以下方式:

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就都不會報錯了。

以上是自己在建項目中碰到的一個問題。

到此這篇關(guān)于詳解python關(guān)于多級包之間的引用問題的文章就介紹到這了,更多相關(guān)python 多級包引用問題內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python使用py2neo操作圖數(shù)據(jù)庫neo4j的方法詳解
  • python實現(xiàn)日歷效果
  • python利用文件讀寫編寫一個博客
  • Python爬蟲技術(shù)
  • 一些讓Python代碼簡潔的實用技巧總結(jié)
  • Python學(xué)習(xí)開發(fā)之圖形用戶界面詳解
  • Python使用Py2neo創(chuàng)建Neo4j的節(jié)點、關(guān)系及路徑

標(biāo)簽:海南 盤錦 漯河 普洱 林芝 南平 大同 寧夏

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解python關(guān)于多級包之間的引用問題》,本文關(guān)鍵詞  詳解,python,關(guān)于,多級,包之,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《詳解python關(guān)于多級包之間的引用問題》相關(guān)的同類信息!
  • 本頁收集關(guān)于詳解python關(guān)于多級包之間的引用問題的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    石首市| 九龙城区| 斗六市| 岳西县| 贵定县| 平乐县| 嫩江县| 进贤县| 遂平县| 柘城县| 东城区| 荥经县| 耿马| 郓城县| 收藏| 微山县| 武隆县| 长子县| 五大连池市| 龙泉市| 嘉荫县| 高密市| 会昌县| 万盛区| 嘉峪关市| 恭城| 德格县| 甘谷县| 浪卡子县| 和政县| 九龙县| 长乐市| 于田县| 岳阳市| 军事| 苍山县| 哈巴河县| 寿宁县| 鄄城县| 菏泽市| 株洲县|