ModuleNotFoundError


what causes ModuleNotFoundError?

ModuleNotFoundError is raised when Python cannot find a module that is given in an import statement. A Python module is a file that ends in .py. Any folder that contains an __init__.py is also a Python module


preview

Here 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
12
13# Exceptions seen
14# AssertionError
15# 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 the error 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 and the test passes

  • 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 terminal shows green again

  • I close the file

  • I add one last failing import statement for practice 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


close the project

  • I close the file(s) I have open in the editor(s)

  • I click in the terminal and exit the tests with ctrl+c on the keyboard

  • I deactivate the virtual environment

    deactivate
    

    the terminal goes back to the command line, (.venv) is no longer on the left side

    .../pumping_python/module_not_found_error
    
  • I change directory to the parent of module_not_found_error

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory


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

Would you like to test AttributeError?


rate pumping python

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