Reputation: 1034
Here is the project structure:
my_module/
├── pytest.ini
├── README
├── requirements.txt
├── my_module
│ ├── __init__.py
│ ├── my_module.py
│ └── utils
│ ├── helpers.py
│ ├── __init__.py
│ └── logger.py
└── tests
├── __init__.py
├── requirements.txt
└── test_my_module.py
I don't understand why I get an ModuleNotFoundError: No module name 'utils'
when I run pytest
in the root directory.
Here is the error
________________________________________________________________ ERROR collecting tests/test_robomaster.py _________________________________________________________________
ImportError while importing test module '/home/heyyou/git/my_module/tests/test_my_module.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib64/python3.6/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/test_my_module.py:3: in <module>
from my_module import my_module
my_module/my_module.py:6: in <module>
from utils import *
E ModuleNotFoundError: No module named 'utils'
It works when I add an __init__.py
at the root level (same as readme) but I don't think it's a good workaround.
The imports work if I run the my_module.py
script directly.
Upvotes: 0
Views: 405
Reputation: 32053
You need to do import like from my_module.utils import *
. Because pytest
insert my_module
to the front of sys.path
Upvotes: 1