0

I am working on my first python project, and I would like to structure the folders correctly. What I have so far is :

HomeDir/MyProject/
        ConfigHandler.py
HomeDir/Tests/
        ConfigHanderTests.py

In the ConfigHanderTests.py I am importing a module defined in ConfigHandler.py called ConfigHandler :

from ConfigHandler import ConfigHandler

But when I run the tests using :

python -m unittest ConfigHandlerTests

I get the following error :

ModuleNotFoundError: No module named 'ConfigHandler'

I have read that I need an init.py in the test folder? But other posts mention that is no longer required. If I just have all the python files and test files in one directory, it works fine. But splitting out into folders doesn't work. What am I missing?

Thanks!

6
  • tests folder should be inside your main project. move Tests/ConfigHanderTests.py inside MyProject/ and change your imports accordingly. Commented Feb 6, 2024 at 18:42
  • That doesn't really feel like "the correct structure". Create a project folder, and nest everything that's relevant to that project in that project folder (venv, tests, docs, etc). Commented Feb 6, 2024 at 18:53
  • from config_handler.simple import Simple and from config_handler.advanced import Advanced Commented Feb 6, 2024 at 19:58
  • So should I just have one folder, and everything lives within that? I wanted to separate the tests into their own folder... Commented Feb 6, 2024 at 20:39
  • Is this what you're looking for? Python import src modules when running tests. Note that there are various different answers. Commented Apr 27, 2024 at 1:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.