ModuleNotFoundError:从其他文件导入类

2024-09-29 03:39:26 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我保存了这段代码,然后打开了一个新文件并键入了这段代码

from student import student

student1 = student('jake','business',3.1,False)

print(student1.name)

我犯了这个错误

ModuleNotFoundError:没有名为“student”的模块

我处理了pip路径并将其粘贴到命令提示符中,然后添加到路径\pip install student中

我犯了这个错误

Could not find a version that satisfies the requirement student (from versions: none)

找不到学生的匹配分布

ps:我正在使用python IDLE


Tags: pip代码namefromself路径ison
2条回答

将类实现保留在students.py中,然后在其他文件(file1.py)中导入student类:

enter image description here

只需在同一文件夹中创建一个空的新文件__init__.py

相关问题 更多 >