Pytest in a nested directory

Viewed 39

This example works if tool_1/ is the top folder. However, I must make room in the repo for other tools, as so:

tool_repo/tool_1/src/main.py
tool_repo/tool_1/test/test_main.py

I also have:

tool_repo/tool_1/__init__.py
tool_repo/tool_1/src/__init__.py
# main.py
def func():
    print("func")

# test_main.py
from tool_1.src.main import func
def test_func():
    func()
    assert 0
Running in: tool_repo\tool_1
>python -m pytest

Error:

ImportError while importing test module 'C:\pycharmProjects\tool_repo\tool_1\test\test_main.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
C:\miniconda3\envs\py39\lib\importlib\__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test\test_main.py:1: in <module>
    from tool_1.src.main import func
E   ModuleNotFoundError: No module named 'tool_1'

Any ideas?

1 Answers

You need to run the module from outside.

If your module tool_1 located in tool_repo\tool_1\, you should run it from:

cd C:\pycharmProjects\tool_repo
python -m pytest

note that your imports looks for it: import tool_1...

Related