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
requirements¶
I open a terminal to run makePythonTdd.sh with
module_not_found_erroras the name of the project./makePythonTdd.sh module_not_found_erroron Windows without Windows Subsystem for Linux use makePythonTdd.ps1 instead of makePythonTdd.sh
./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) oroption or command(MacOS) on the keyboard and use the mouse to click ontests/test_module_not_found_error.py:7to open it in the editor,then I change True to False to make the test pass
7 self.assertFalse(False)I change the name of the class to match the CapWords format to follow Python convention
4class TestModuleNotFoundError(unittest.TestCase):
test_module_not_found_error¶
RED: make it fail¶
I change
test_failuretotest_module_not_found_error1import 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.pyin thesrcfolderI add the error to the list of Exceptions encountered in
test_module_not_found_error.py10# Exceptions Encountered 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.py6 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.pyin thesrcfolder and the test passesI close the file
I continue with another import statement in
test_module_not_found_error.py6 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.pyto thesrcfolder and the terminal shows green againI close the file
I add one last failing import statement for practice in
test_module_not_found_error.py6 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
srcfolder and the test passesI close the file
review¶
I ran a test for ModuleNotFoundError to practice making Python modules. Would you like to test AttributeError?
Click Here for the code I wrote in this chapter