[英]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: testing2模块要求我使用for循环遍历菜单,没有硬编码的代码,但是我得到了:
AttributeError: module 'testing2' has no attribute 'printPackage'
Help and suggestion please. 请帮助和建议。 I'm just started learning python.
我刚刚开始学习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. 如果看一下
testing1
第1行,则会看到一个import语句。 This then runs testing2
. 然后运行
testing2
。 The first line of this module is to import testing1
. 该模块的第一行是导入
testing1
。 When Python runs this module, it finds a reference to testing2.printPackage
, which has not been imported yet. 当Python运行此模块,它找到一个参考
testing2.printPackage
, 尚未进口的呢。
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: 另一种选择是,在
testing2
的import语句testing2
,是这样定义printPackage
函数:
printPackage = lambda menuList: None
and then continue your module, later on re-defining the printPackage
function. 然后继续您的模块,稍后重新定义
printPackage
函数。
Hopefully this will then work. 希望这会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.