简体   繁体   中英

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

I have this first module:

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

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

and the second module:

#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

''')

the testing2 module requires me to use a for loop to loop through the menu and no hardcoded codes but i get the:

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

Help and suggestion please. I'm just started learning python. Thanks.

From what I can see, the reason that you are getting the error is because both modules are reliant on the other.

If you have a look at line 1 of testing1 , you see an import statement. This then runs testing2 . The first line of this module is to import testing1 . When Python runs this module, it finds a reference to testing2.printPackage , which has not been imported yet.

To fix this, try to sort out the dependencies of both modules, and see if you can combine both into one module.

The alternative option is, before the import statement in testing2 , is to define the printPackage function as so:

printPackage = lambda menuList: None

and then continue your module, later on re-defining the printPackage function.

Hopefully this will then work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM