ModuleNotFoundError



ModuleNotFoundError is raised when Python cannot find a module from an import statement. A Python module has a filename that ends in .py

test_module_not_found_error

red: make it fail

  • I open a terminal to run makePythonTdd.sh with module_not_found_error as the name of the project

    ./makePythonTdd.sh module_not_found_error
    

    on Windows without Windows Subsystem Linux use makePythonTdd.ps1

    ./makePythonTdd.ps1 module_not_found_error
    

    it makes the folders and files that are needed, installs packages, runs the first test, and the terminal shows AssertionError

    E       AssertionError: True is not false
    
    tests/test_module_not_found_error.py:7: AssertionError
    
  • I hold ctrl (windows/linux) or option (mac) on the keyboard and use the mouse to click on tests/test_module_not_found_error.py:7 to open it in the editor, then change test_failure to test_module_not_found_error

    import unittest
    
    
    class TestModuleNotFoundError(unittest.TestCase):
    
        def test_module_not_found_error(self):
            import src.module_00
    

    and the terminal shows ModuleNotFoundError

    ModuleNotFoundError: No module called 'src.module_00'
    

    because Python cannot find module_00.py in the src folder

  • I add the error to the list of Exceptions encountered

    # Exceptions Encountered
    # AssertionError
    # ModuleNotFoundError
    

green: make it pass

  • then I rename module_not_found_error.py in the src folder to module_00.py and the terminal shows a passing test

  • I add another import statement

    def test_module_not_found_error(self):
        import src.module_00
        import src.module_01
    

    which gives me ModuleNotFoundError

    ModuleNotFoundError: No module named 'src.module_01'
    
  • When I make a new file named module_01.py in the src folder, the terminal shows a passing test

  • I continue with another import statement

    def test_module_not_found_error(self):
        import src.module_00
        import src.module_01
        import src.module_02
    

    and get ModuleNotFoundError

    ModuleNotFoundError: No module called 'src.module_02'
    
  • I add module_02.py to the src folder, and the terminal shows green again

  • one last failing import statement

    def test_module_not_found_error(self):
        import src.module_00
        import src.module_01
        import src.module_02
        import src.module_03
    

    and the terminal shows

    ModuleNotFoundError: No module called 'src.module_03'
    
  • I add the file to the src folder and the test passes


review

I ran a test for the ModuleNotFoundError and Python modules. Would you like to test the AssertionError?


ModuleNotFoundError: test