繁体   English   中英

'.' 的目的是什么? 从另一个 py 文件导入类时?

[英]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中运行。

  1. student.py

     class Student: def __init__(self,name, number): self._student_name = name self._student_number = number
  2. 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
  3. __init__.py

     from.student import Student from.course import Course
  4. 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文件中工作?

单个前导点表示相对导入,从当前的 package 开始。

您需要在当前上下文中拥有这些模块才能像这样导入它们

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM