我創建了一個類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
回溯(最近通話最後一個): 文件「/用戶/ Dropbox的/使用Python 3/* PY」,2號線,在 從矩形進口矩形 導入錯誤:沒有模塊名爲矩形 –
是包含了Python文件定義名爲'Rectangle.py'的'Rectangle'類?該文件是否在你的Python路徑中,即在'sys.path'列表中的一個目錄中? –