簡體   English   中英

如何修復AttributeError:模塊'testing2'沒有屬性'printPackage'?

[英]How to fix AttributeError: module 'testing2' has no attribute 'printPackage'?

我有第一個模塊:

#testing1
import testing2
choice = input('Enter your choice:')

def calculateMenuPrice(choice):
    testing2.printPackage(menuList)
calculateMenuPrice(choice)

第二個模塊:

#testing2
import testing1
menuList = testing1.calculateMenuPrice(choice)
def printPackage(menuList):
    for x in menuList:
        if menuList == '1':
            return('''
----------
Menu List
----------
1. Jelly Fish Yee Sang with Pear
2. Dried Seafood with Fish Soup 
3. Steamed Sea Water Grouper

''')        
        elif menuList == '2':
             return('''
----------
Menu List
----------
1. Jelly Fish Yee Sang with Pear
2. Shark Fin Soup with Crab Meat
3. Steamed River Patin Fish
''')
        elif menuList == '3':
            return('''
----------
Menu List
----------
1. Salmon Fish Yee Sang with Pear
2. Steamed Classic Abalone Soup
3. Steamed Bamboo Fish

''')

        elif menuList == '4':
            return('''
----------
Menu List
----------
1. Abalone Yee Sang with Pear
2. Mini Classic Steam Soup
3. Steamed Local Pomfret Fish

''')

testing2模塊要求我使用for循環遍歷菜單,沒有硬編碼的代碼,但是我得到了:

AttributeError: module 'testing2' has no attribute 'printPackage'

請幫助和建議。 我剛剛開始學習python。 謝謝。

從我所看到的,您得到錯誤的原因是因為兩個模塊都依賴於另一個。

如果看一下testing1第1行,則會看到一個import語句。 然后運行testing2 該模塊的第一行是導入testing1 當Python運行此模塊,它找到一個參考testing2.printPackage尚未進口的呢。

要解決此問題,請嘗試整理兩個模塊的依賴關系,然后查看是否可以將兩個模塊組合到一個模塊中。

另一種選擇是,在testing2的import語句testing2 ,是這樣定義printPackage函數:

printPackage = lambda menuList: None

然后繼續您的模塊,稍后重新定義printPackage函數。

希望這會起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM