简体   繁体   中英

What's the purpose of '.' when importing classes from another py file?

I have created 2 Python files and it's saved in the OOP_1 folder. There are errors when I tried to run and I am unable to understand the purpose of using . when importing the classes from Python files.

I have tried removing the .student , .course , from the relevant Python files, but it wouldn't run in main.py using the from OOP_1 import * .

  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 *

However, in __init__ , the Python gives me an error:

ModuleNotFoundError: No module named '__main__.student'; '__main__' is not a package

In main.py , Python gives me:

ModuleNotFoundError: No module named 'course'

What can I do to ensure that my folder where I stored my classes will work in main.py file?

A single leading dot indicates a relative import, starting with the current package.

You need to have those modules in the current context to import them like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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