what is a module?


A Python module is a file that ends in .py. Any folder that contains an __init__.py is also a Python module

what causes ModuleNotFoundError?

ModuleNotFoundError is raised when Python cannot find a module given in an import statement


preview

These are the tests I have by the end of the chapter

 1import unittest
 2
 3
 4class TestModuleNotFoundError(unittest.TestCase):
 5
 6    def test_module_not_found_error(self):
 7        import src.module_00
 8        import src.module_01
 9        import src.module_02
10        import src.module_03
11        import src.module_04
12        import src.module_05
13        import src.module_06
14        import src.module_07
15        import src.module_08
16        import src.module_09
17
18
19# Exceptions seen
20# AssertionError
21# ModuleNotFoundError

start the project


test_module_not_found_error


RED: make it fail


  • I change test_failure to test_module_not_found_error

    1import unittest
    2
    3
    4class TestModuleNotFoundError(unittest.TestCase):
    5
    6    def test_module_not_found_error(self):
    7        import src.module_00
    

    the terminal shows ModuleNotFoundError

    ModuleNotFoundError: No module called 'src.module_00'
    

    because Python cannot find module_00.py in the src folder

  • I add ModuleNotFoundError to the list of Exceptions seen in test_module_not_found_error.py

    10# Exceptions seen
    11# AssertionError
    12# ModuleNotFoundError
    

GREEN: make it pass


I change module_not_found_error.py in the src folder to module_00.py and the test passes


REFACTOR: make it better


  • I add another import statement to test_module_not_found_error.py

    6    def test_module_not_found_error(self):
    7        import src.module_00
    8        import src.module_01
    

    the terminal shows ModuleNotFoundError

    ModuleNotFoundError: No module named 'src.module_01'
    
  • I make a new file named module_01.py in the src folder, the test passes and I close the file

  • I continue with another import statement in test_module_not_found_error.py

    6    def test_module_not_found_error(self):
    7        import src.module_00
    8        import src.module_01
    9        import src.module_02
    

    the terminal shows ModuleNotFoundError

    ModuleNotFoundError: No module called 'src.module_02'
    
  • I add module_02.py to the src folder and the test is green again again, I close the file

  • I add another import statement in test_module_not_found_error.py

     6    def test_module_not_found_error(self):
     7        import src.module_00
     8        import src.module_01
     9        import src.module_02
    10        import src.module_03
    

    the terminal shows

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

  • I add an import statement to test_module_not_found_error.py

    10        import src.module_03
    11        import src.module_04
    12
    13
    14# Exceptions seen
    

    the terminal shows ModuleNotFoundError

    ModuleNotFoundError: No module named 'src.module_04'
    
  • I add module_04.py to the src folder, the test passes and I close the file

  • I add another import statement to test_module_not_found_error.py

    11        import src.module_04
    12        import src.module_05
    13
    14
    15# Exceptions seen
    

    the terminal shows ModuleNotFoundError

    ModuleNotFoundError: No module named 'src.module_05'
    
  • I add the file to the src folder, the test passes and I close the file

  • another import statement in test_module_not_found_error.py

    12        import src.module_05
    13        import src.module_06
    14
    15
    16# Exceptions seen
    

    the terminal shows ModuleNotFoundError

    ModuleNotFoundError: No module named 'src.module_06'
    
  • I add module_06.py to the src folder, the test passes, and I close the file

  • I add an import statement in test_module_not_found_error.py

    13        import src.module_06
    14        import src.module_07
    15
    16
    17# Exceptions seen
    

    the terminal shows ModuleNotFoundError

    ModuleNotFoundError: No module named 'src.module_07'
    
  • I add the module to the src folder, the test passes and I close the file

  • I add an import statement to test_module_not_found_error.py

    14        import src.module_07
    15        import src.module_08
    16
    17
    18# Exceptions seen
    

    the test passes

  • I add module_08.py to the src folder, the test passes

  • I add the last import statement to test_module_not_found_error.py

    15        import src.module_08
    16        import src.module_09
    17
    18
    19# Exceptions seen
    

    the terminal shows ModuleNotFoundError

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


close the project

  • I close test_module_not_found_error.py in the editor

  • I click in the terminal, then use q on the keyboard to leave the tests. The terminal goes back to the command line


review

I ran a test for ModuleNotFoundError to practice making Python modules


code from the chapter

Do you want to see all the CODE I typed in this chapter?


what is next?

you have gone through a lot of information and know

Are you ready for a review, How many questions do you think you can answer?


rate pumping python

If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too