Ich bin neu in Python und ich versuche, ein Modul und eine Klasse zu erstellen.Python-Modul und Klasse - AttributeError: Modul hat kein Attribut
Wenn ich versuche, mystuff
zu importieren und dann cfcpiano = mystuff.Piano()
verwenden, erhalte ich eine Fehlermeldung:
AttributeError: module 'mystuff' has no attribute 'Piano'
Wenn ich von mystuff import Piano
versuche ich bekommen:
ImportError: cannot import name 'Piano'
Kann jemand erklären, was los ist? Wie verwende ich ein Modul und Klasse in 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()
Haben Sie eine '__init __. Py' Datei im' mystuff' Verzeichnis? –
Können wir Ihre komplette Dateihierarchie sehen? –
'Test.py' und' mystuff.py' müssen sich im selben Ordner befinden, damit der Code so funktioniert wie er ist. –