简体   繁体   中英

How to fix AttributeError:partially initialized module (Visual Studio 2019)

I'm running the following Python code in Visual Studio:


app.py |

from Student import Student

student1 = Student("Jim", "Business", 3.1, False)

print(student1.gpa)

Student.py |

class Student:
    def __init__(self, name, major, gps, is_on_probation):
        self.name = name
        self.major = major
        self.gpa = gpa
        self.is_on_probation = is_on_probation

When I try to run it, VS throws the error:

cannot import name 'Student' from partially initialized module 'Student' (most likely due to a circular import) (C:\Users\cdegr\source\repos\Student\Student.py)

There was a similar question that I saw, but there wasn't any fix posted for the original question. Only requests to see the code posted, so here's mine. Why is my code not working?

Your class name and your python file name are the same (Student). This causes the error, and the suspicion of circular imports. Rename the class or the python file name, and app.py should work.

Sidenote: in the __init__ function, your gpa parameter is spelled as gps , so you should fix that.

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