Home > Mobile >  ModuleNotFoundError while running unit tests
ModuleNotFoundError while running unit tests

Time:08-12

With such a project structure:

- code
  - modules
    - tests
      test_data_quality_service.py
    data_quality_service.py
  - web

I keep getting errors when I try to import class DataQualityChecker from data_quality_service. I get these erros both for absolute

from code.modules.data_quality_service import DataQualityChecker
E   ModuleNotFoundError: No module named 'code.modules'; 'code' is not a package

and relative imports:

from ..data_quality_service import DataQualityChecker
E   ImportError: attempted relative import with no known parent package

How can I fix it? It's happening in PyCharm, I'm running unit tests and I also tried pytest but I had the same error.

CodePudding user response:

You need to add an __init__.py file (it can be empty) on each folder to mark it as a python package.

- code
  - modules
    - tests
      test_data_quality_service.py
      __init__.py
    data_quality_service.py
    __init__.py
  - web

Also, probably you should consider a better project structure

CodePudding user response:

In python installation directory there is file python38._pth You have to give module path code.modules

  • Related