Python错误导入类另一个目录

时间:2018-09-27 13:42:08

标签: python

我有结构项目python

MyProject
+Classes
    -stage_competition.py
main.py

正在执行Python(main.py)-显示错误“ ModuleNotFoundError(“没有名为'Classes','的模块”)

我尝试直接导入,但是也无法正常工作,总是显示相同的错误

from Classes.stage_competition import Stage_competition

item = Stage_competition("field1", "field2", "field3" , "field4")


Create class Stage_competition

class Stage_competition(object):
    """description of class"""

    def __init__(self, type_competition, datalake, competition ,hour):
        self.type_competition = type_competition
        self.datalake = datalake
        self.competition = competition
        self.hour = hour

    def settype_competition(self, type_competition):
        self.type_competition = type_competition

    def setdatalake(self, datalake):
        self.datalake = datalake

    def getcompetition(self):
        return self.competition

    def gethour(self):
        return self.hour

1 个答案:

答案 0 :(得分:1)

如果STATIC_URL = '/static/' STATIC_ROOT = "/static/" STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"),] Classes位于同一文件夹中,则应以这种方式导入它(注意在类之前的点):

main.py

或者,您可以将目录从from .Classes.stage_competition import Stage_competition 添加到Classes,从而可以从任何地方PYTHONPATH