Я создал класс Rectangle и попытаюсь сохранить его в модуле.Запустить модуль в python 2.7.6?
Выход:
Traceback (most recent call last):
File "/Users/Dropbox/using Python 3/test.py", line 2, in <module>
from Rectangle import Rectangle
ImportError: No module named Rectangle
Мой код:
import math
class Rectangle:
def __init__(self, width = 1 , height = 2):
self.width = width
self.height = height
def getPerimeter (self):
return (self.width + self.height) * 2
def getArea (self):
return self.width * self.height
def setSides(self, width, height):
self.width = width
self.height = height
Еще один файл запуска для проверки Прямоугольник модуль:
from Rectangle import Rectangle
def main():
Rectangle1 = Rectangle(4, 40)
print "The area of the rectangle,", Rectangle1.width, "is width and", Rectangle1.height, "is height, are", Rectangle1.getArea()
print "The perimeter of the rectangle,", Rectangle1.width, "is width and", Rectangle1.height, "is height, are", Rectangle1.getPerimeter()
Rectangle2 = Rectangle(3.5, 35.7)
print "The area of the rectangle,", Rectangle2.width, "is width and", Rectangle2.height, "is height, are", Rectangle2.getArea()
print "The perimeter of the rectangle,", Rectangle2.width, "is width and", Rectangle2.height, "is height, are", Rectangle2.getPerimeter()
main()
Хорошо. Так в чем проблема? – thefourtheye
Traceback (самый последний вызов последнего): Файл "/ Users/Dropbox/с помощью Python 3/* ру.", Строка 2, в из Прямоугольник импорта Прямоугольник ImportError: Нет модуль с именем Rectangle –
ли файл Python, содержащий определение вашего класса 'Rectangle' с именем' Rectangle.py'? Этот файл находится в вашем пути Python, т. Е. В одной из каталогов в списке 'sys.path'? –