简体   繁体   中英

Why can't I run my python file from another?

 import student

^Is at the top of my file.

self.studentPage = student.WindowStudent()

Is being run from in a function in a class. WindowStudent is the function to be run in the file being opened in student.py

The class to be run is set up like this...

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

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

This is my error? Does python think the file I'm importing is a module?

TIA for any help.

Wrong file

If the import is succeeding but it's not finding the class defined in the file, it could be that you're importing the wrong file.

Where you have your import statement, add a line to print the file path location of the imported moduled, as follows:

import student
print(student.__file__)

Check the path that is output and compare to the file you want to import.

Wrong indentation

Check the indentation on your class WindowStudent in the student.py file. If it is indented incorrectly it may appear as a child of another class/function, and so not be available in the module outer scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM