为什么我不能从另一个运行我的python文件?

时间:2017-09-13 17:06:12

标签: python import

 import student

^位于我的文件顶部。

self.studentPage = student.WindowStudent()

从类中的函数运行。 WindowStudent是要在student.py

中打开的文件中运行的函数

要运行的类设置如下......

class WindowStudent(QtGui.QMainWindow):
    def __init__(self):
        ETC

AttributeError: 'module' object has no attribute 'WindowStudent'

这是我的错误? python认为我导入的文件是一个模块吗?

TIA提供任何帮助。

1 个答案:

答案 0 :(得分:1)

文件错误

如果导入成功但是没有找到文件中定义的类,则可能是您导入了错误的文件。

如果您有import语句,请添加一行以打印导入的模块的文件路径位置,如下所示:

import student
print(student.__file__)

检查输出的路径并与要导入的文件进行比较。

错误的缩进

检查WindowStudent文件中的班级student.py上的缩进。如果缩进不正确,它可能显示为另一个类/函数的子节点,因此在模块外部范围内不可用。

相关问题