我是新来的python,我试图创建一个模块和类。Python模块和类 - AttributeError:模块没有属性
如果我尝试导入mystuff
然后用cfcpiano = mystuff.Piano()
,我得到一个错误:
AttributeError: module 'mystuff' has no attribute 'Piano'
如果我从mystuff import Piano
尝试,我得到:
ImportError: cannot import name 'Piano'
有人能解释这是怎么回事?如何使用模块和类在Python
mystuff.py
def printhello():
print ("hello")
def timesfour(input):
print (input * 4)
class Piano:
def __init__(self):
self.type = raw_input("What type of piano? ")
def printdetails(self):
print (self.type, "piano, " + self.age)
Test.py
import mystuff
from mystuff import Piano
cfcpiano = mystuff.Piano()
cfcpiano.printdetails()
你在'mystuff'目录下有'__init __。py'文件吗? –
我们能否看到您的完整文件层次结构? –
'Test.py'和'mystuff.py'必须位于同一个文件夹中,才能使代码保持原样。 –