是否在python 2.7.6中运行该模块?

是否在python 2.7.6中运行该模块?,python,python-2.7,module,Python,Python 2.7,Module,我创建了一个类矩形并尝试保存到一个模块 输出: 回溯(最近一次呼叫最后一次): 文件“/Users/Dropbox/usingpython3/test.py”,第2行,在 从矩形导入矩形 ImportError:没有名为Rectangle的模块 我的代码: import math class Rectangle: def __init__(self, width = 1 , height = 2): self.width = width self.hei

我创建了一个类矩形并尝试保存到一个模块

输出:

回溯(最近一次呼叫最后一次):
文件“/Users/Dropbox/usingpython3/test.py”,第2行,在
从矩形导入矩形
ImportError:没有名为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 ()

试试这个inside test.py

导入系统

sys.path.append(“矩形类模块路径”)


导入“矩形类模块”

问题不在于代码,而在于文件在目录中的命名和排列方式。由于您的问题目前不包含有关您的文件名以及它们位于哪个目录的信息,因此我们无法进一步帮助您。至少,
Rectangle
类应该位于名为
Rectangle.py
的文件中,并且它需要位于当前目录中,或者位于
PYTHONPATH
上的目录中。也可以有一个名为
Rectangle
的目录,该目录需要包含一个文件
\uuuu init\uuuu.py
,该文件可能为空,或者包含模块范围的Python代码。

固定代码:

从Exercise0701导入矩形

因为我保存的文件名是Exercise0701.py,所以在计算机中找不到矩形的文件名

我犯了错误


谢谢大家。

好的。。那么问题出在哪里呢?回溯(最近一次调用):File“/Users/Dropbox/using Python 3/*.py”,第2行,from Rectangle import Rectangle ImportError:没有名为Rectangle的模块是包含名为
Rectangle.py
的类定义的Python文件?该文件是否在Python路径中,即在
sys.path
列表中的一个目录中?或者,您可以尝试将Rectangle.py和test.py放在同一路径中,如果得到Rectangle.pyc,则尝试运行test.py,test.py应该正常运行。矩形类的python文件名是什么?模块的名称中不能有空格。请将此(或其他答案,如果您愿意)标记为已接受,以便此问题不再被视为未解决。谢谢