通过父目录导入模块符号

时间:2017-03-19 21:11:27

标签: python

我有一个名为work的Python目录,其中包含许多包含与该模块相关的类的模块。例如,student包含Grades, Classes, Attendance。我的目标是代替:

from work.student import Grades

我想简单地调用:

from work import Grades

即,我想将所有内部符号拉入父模块。我意识到我可以在那里放一个名为__init__.py的文件并手动导入所有内容。即from .student import *等。

我想知道是否有更容易和自动的方法。

1 个答案:

答案 0 :(得分:0)

从你的问题我得到如下项目图片:

work/
├── __init__.py
└── student.py

在student.py中,您有课程 - >成绩,班级,出勤

如上所述 Krishna ,您可以在工作目录下的 init .py(或python术语中的模块)中执行以下操作。

__init__.py

**"""
Place any module level settings, env variables, module level declarations, etc  
"""**
from student import Grades, Classes, Attendace

现在您可以将学生模块中的课程导入为:

from work import Grades
from work import Classes
from work import Attendance
相关问题