为什么我不能从父目录导入类?

时间:2018-04-28 02:32:35

标签: python

我在我的python项目上为web部署了一个python模块。模块(callback.py)无法在父目录(logger.py)上的模块中导入该类。但它可以在另一个模块(storekeeper.py)和简单模块(settings.py)中导入该类。

如何在logger.py中使用callback.py导入Logger类?

我收到了以下信息。

Traceback (most recent call last):
  File "web\callback.py", line 21, in <module>
    from logger import Logger
ImportError: cannot import name 'Logger'

我的项目:

project
  |-module
    |-web
      |-__init__.py
      |-callback.py
    |-__init__.py
    |-base.py
    |-logger.py
    |-settings.py
    |-storekeeper.py

callback.py:

#!/usr/bin/python3

import cgi
import os
import sys

sys.path.append(os.curdir)

import settings
from io import StringIO
from storekeeper import StoreKeeper
from logger import Logger


class Callback:

    @classmethod
    def main(cls):

        try:
            # some code
        except Exception as e:
            sys.stdout = StringIO()
            Logger.alert(str(e))


if __name__ == '__main__':
    Callback.main()

logger.py

from base import Base


class Logger(Base):

    def __init__(self):

        Base.__init__(self)

    @classmethod
    def alert(cls, subj):
        # some code

storekeeper.py

class StoreKeeper:

    def __init__(self):

        self.__dic = None

    def get(self, key):
        # some code

0 个答案:

没有答案