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) oroption
(mac) on the keyboard and use the mouse to click ontests/test_module_not_found_error.py:7
to open it in the editor, then changetest_failure
totest_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 thesrc
folderI 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 thesrc
folder tomodule_00.py
and the terminal shows a passing testI 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 thesrc
folder, the terminal shows a passing testI 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 thesrc
folder, and the terminal shows green againone 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?