I have to following project structure:
importTesting
├── importTesting
│ ├── main.py
│ ├── module_a.py
│ └── module_b.py
└── tests
└── test.py
module_a.py
from module_b import foo
def bar(x):
return foo(x)
module_b.py
def foo(x):
return x
main.py
from module_a import bar
test.py
import unittest
from importTesting.module_a import bar
class TestCase(unittest.TestCase):
def test_bar(self):
self.assertEqual(bar(1),1)
I'm trying to make both main.py and test.py run via the command line, but when I run test.py I get this error about module_b not found:
psagot_api) C:\Users\Roy\PycharmProjects\importTesting>python -m unittest
E
======================================================================
ERROR: tests.test (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.test
Traceback (most recent call last):
File "C:\Users\Roy\anaconda3\envs\psagot_api\lib\unittest\loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "C:\Users\Roy\anaconda3\envs\psagot_api\lib\unittest\loader.py", line 377, in _get_module_from_name
__import__(name)
File "C:\Users\Roy\PycharmProjects\importTesting\tests\test.py", line 2, in <module>
from importTesting.module_a import bar
File "C:\Users\Roy\PycharmProjects\importTesting\importTesting\module_a.py", line 1, in <module>
from module_b import foo
ModuleNotFoundError: No module named 'module_b'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
any suggestions ? Including changing project structure.
EDIT-1: Every once in a while I suffer from python's import mechanism. The folder containing the imports must be in sys.path.
Some possibilities that can resolve the issue :
1.Add the importTesting/importTesting to sys.path from within the code
2.Install as package with pip and then change imports to importTesting.something.
3.Run python -m pytest from the folder containing main.py Running with -m tag adds the current did to sys.path
4.Add a file named conftest.py to the same dir as main.py when running pytest from project root will add the folder containing conftest to sys.path.
Got to say i'm not completely satisfied with any of these
Thanks Roy