[英]What's the purpose of '.' when importing classes from another py file?
我创建了 2 个 Python 文件,并将其保存在OOP_1
文件夹中。 尝试运行时出现错误,无法理解使用.
从 Python 文件导入类时。
我尝试从相关的 Python 文件中删除.student
, .course
,但它不会使用from OOP_1 import *
在main.py
中运行。
student.py
class Student: def __init__(self,name, number): self._student_name = name self._student_number = number
course.py
from.student import Student class Course(Student): #instance attributes def __init__(self, name, code, credit, student_limit): # Your code for the constructor self._name = name self._code = code self._credit = credit self._student_limit = student_limit
__init__.py
from.student import Student from.course import Course
main.py
from OOP_1 import *
但是,在__init__
中, Python 给了我一个错误:
ModuleNotFoundError: No module named '__main__.student'; '__main__' is not a package
在main.py
中, Python 给了我:
ModuleNotFoundError: No module named 'course'
我可以做些什么来确保我存储类的文件夹可以在main.py
文件中工作?
您需要在当前上下文中拥有这些模块才能像这样导入它们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.