python - '.' 的目的是什么?从另一个 py 文件导入类时?
问题描述
我创建了 2 个 Python 文件,并将其保存在OOP_1
文件夹中。当我尝试运行时出现错误,我无法理解.
从 Python 文件导入类时使用的目的。
我已经尝试从相关的 Python 文件中删除.student
, .course
,但它不会在main.py
使用from OOP_1 import *
.
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
文件中工作?
解决方案
您需要在当前上下文中拥有这些模块才能像这样导入它们
推荐阅读
- c++ - std::initializer_list 是否有复制构造函数并且曾经使用过?
- java - 将二进制文件保存到 /<
>/安卓/数据/< > - memory-management - 访问结构时如何避免分配?朱莉娅
- jquery - 如何将页面重定向到另一个页面并在jQuery中的重定向页面上执行代码?
- tensorflow - 如何在张量流中计算 f1
- php - PHP 不会无缘无故发送消息
- c++11 - 是所有数据成员都初始化为 0 还是由自动调用的构造函数分配随机值?
- javascript - 如何使 Orbit Controls 在不同的轴上自动旋转?
- c++ - 如何使用 Opencv 在任何计算机上显示图像
- java - SQLite - 查询将日期时间与 WHERE 子句进行比较的任何行时出现日期时间格式的问题