''ModuleNotFoundError: No module named.....' error while running the pytest in other directory

I'm using Python 3.7.5 and pytest 7.0.1 on Linux. I am writing unit test cases of my project in /tests directory and executing it by following command parent directory, it ran successfully.

python3 -m pytest tests/xxx/test_xxx.py

However, when i copy the same script in /pytest directory and ran the same script again, it ended up with an error.

python3 -m pytest pytests/xxx/test_xxx.py

Error:

ImportError while importing test module
'/projects/db/analytics/advanced_analytics/advanced_analytics/pytests/xxx/test_xxx.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback: /opt/python3.7/lib/python3.7/importlib/__init__.py:127: in
import_module
return _bootstrap._gcd_import(name[level:], package, level) pytests/xxx/test_xxx.py:21: in <module>
from myproject.my_project import my_project my_project/my_project.py:13: in <module>
from advanced_analytics.framework.CICLogger import CICLogger  E   ModuleNotFoundError: No module named 'advanced_analytics.framework'

Please note my_project , /tests and /pytests are created in same parent directory - advanced_analytics.

from advanced_analytics.framework.CICLogger import CICLogger imported in my_project.py file

Kindly let me know why this issue is occurring and how to fix the issue.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source