pythonunit-testingimport

Python import src modules when running tests


My source files are located under src and my test files are located under tests. When I want to run a test file, say python myTest.py, I get an import error: "No module named ASourceModule.py".

How do I import all the modules from source needed to run my tests?


Solution

  • You could add that directory to the path:

    import sys
    sys.path.append('../src')
    

    Maybe put this into a module if you are using it a lot.

    Although this solution works for some situations, there are better solutions. See for example this answer.