[英]How to fix AttributeError:partially initialized module (Visual Studio 2019)
I'm running the following Python code in Visual Studio:我在 Visual Studio 中运行以下 Python 代码:
from Student import Student
student1 = Student("Jim", "Business", 3.1, False)
print(student1.gpa)
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:当我尝试运行它时,VS 会抛出错误:
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).您的 class 名称和您的 python 文件名相同(学生)。 This causes the error, and the suspicion of circular imports.
这会导致错误,并怀疑循环导入。 Rename the class or the python file name, and app.py should work.
重命名 class 或 python 文件名,app.py 应该可以工作。
Sidenote: in the __init__
function, your gpa
parameter is spelled as gps
, so you should fix that.旁注:在
__init__
function 中,您的gpa
参数拼写为gps
,因此您应该修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.