I am trying to test a package with the following structure:
.
├── src
├──--myproject
│ ├── __init__.py
│ ├── module1.py
│ ├── module2.py
│ └── main.py
├──-tests
├──test_simple.py
├──test_module1.py
I am running with a virtualenv.
#test_simple.py
def test_tests_working() -> None:
assert 1 == 1
#test_module1.py
import sys
sys.path.append(r"path_to_projectsrc") #i got this from another SO answer but didn't seem to resolve anything
from myproject.module1 import func1
def test_func1() -> None:
blah = func1(args)
assert blah == "expected func1 results"
test_simple
works fine, returns True, so I know pytest itself is working okay and its looking in the right places so and so forth, sanity checks check out. However, when I get to test_module1
, I get an error on line 1 because in module1
it’s trying to import another module, which causes failure:
E ModuleNotFoundError: No module named 'module2'
#module1.py
import module2
def func1():
#do stuff
Why is it not working? If I run the scripts directly, or even from another script, I have no issues with the import statements being the way they are. I tried to change to from . import module2
but that didn’t solve it. I deleted the tests/__init__.py
file on a another SO recommendation as well, but to no avail.