0

Just set up my new macbook and I am having importIssues running my python tests.

pytest --cov-config=.coveragerc --cov=main tests

I get the error:

Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_rds_config.py:6: in <module>
    from main import rds_config
E   ModuleNotFoundError: No module named 'main'

Running the same tests on my old laptop works fine. Some people have said I need to do something with my PYTHONPATH but I am not sure what to do. I have installed python 3.9 using brew.

Also, running the test in pycharm by pressing the run button also works fine. So it is not working only in the terminal.

  1. File structure:
    • Project
      • main
        • lambda_function
        • rds_config
      • tests
        • test_lambda_function
        • test_rds_config

How I am importing: from main import lambda_function

L44TXF
  • 65
  • 3
  • 10
  • 1
    Its probably about how you have structured your project, sound like you have some kind of source folder and a test folder. Check https://stackoverflow.com/questions/70172116/modulenotfounderror-no-module-named/70172229#70172229 – Tzane Dec 08 '21 at 12:43
  • Post your folder structure and the test file to find the root cause of the issue. – Ramachandran Rajasekaran Dec 08 '21 at 12:50
  • 1
    Try running tests with `python -m pytest ...` so your project root is also added to python path https://docs.pytest.org/en/6.2.x/usage.html#calling-pytest-through-python-m-pytest – Tzane Dec 08 '21 at 12:56
  • @Tzane Thank you for the links will check them out now and will report back. – L44TXF Dec 08 '21 at 12:58
  • @RamachandranRajasekaran have added the project structure now – L44TXF Dec 08 '21 at 12:58
  • @Tzane adding python -m did the trick! thank you. Do you know if there is any pre config that I have missed that would prevent me from needing to add python -m – L44TXF Dec 08 '21 at 13:03
  • You could add a `conftest.py` to test folder root that adds the project root to python path, but I think you should just use the documented solution of running with `python -m pytest` – Tzane Dec 08 '21 at 13:05
  • Please refer the link provided by @Tzane. The solution in that link will add the main folder to your source and can import it in the tests. – Ramachandran Rajasekaran Dec 08 '21 at 13:26

0 Answers0