0

I deployed a python module for web on my python project. The module(callback.py) can't import the class in a module on parent directory(logger.py). But it can import the class in another module(storekeeper.py) and the simple module(settings.py).

How can callback.py import Logger class in logger.py?

I got the message below.

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

My project:

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
isexxx
  • 727
  • 1
  • 6
  • 23
  • What happens if you try `import logger`? Clearly `from storekeeper ...` is resolving. More than likely you are running the `callback.py` and so only `/project/module/web` is on the search path, not `/project/module`. But if `from storekeeper ...` is resolving...I'm not sure. You can read [this](https://stackoverflow.com/questions/48304317/how-does-implicit-relative-imports-work-in-python) post for more information. If it is the case, just do `from .logger import Logger`. – pstatix Apr 28 '18 at 02:40
  • Tnx, pstatix. It resolved by PYTHONPATH as same as "Importing modules from parent folder". – isexxx Apr 28 '18 at 21:35

0 Answers0