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
I open a terminal to run makePythonTdd.sh with
module_not_found_erroras the name of the project./makePythonTdd.sh module_not_found_errorNote
on Windows without Windows Subsystem for Linux use makePythonTdd.ps1 instead of makePythonTdd.sh
./makePythonTdd.ps1 module_not_found_errorit 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: AssertionErrorI hold ctrl (Windows/Linux) or option/command (MacOS) on the keyboard and use the mouse to click on
tests/test_module_not_found_error.py:7to put the cursor on line 7 in the editorthen I change True to False in the assertion
7 self.assertFalse(False)the test passes
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_00the terminal shows ModuleNotFoundError
ModuleNotFoundError: No module called 'src.module_00'because Python cannot find
module_00.pyin thesrcfolderI add ModuleNotFoundError to the list of Exceptions seen in
test_module_not_found_error.py10# 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.py6 def test_module_not_found_error(self): 7 import src.module_00 8 import src.module_01the terminal shows ModuleNotFoundError
ModuleNotFoundError: No module named 'src.module_01'I make a new file named
module_01.pyin thesrcfolder, the test passes and I close the fileI 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_02the terminal shows ModuleNotFoundError
ModuleNotFoundError: No module called 'src.module_02'I add
module_02.pyto thesrcfolder and the test is green again again, I close the fileI add 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 10 import src.module_03the terminal shows
ModuleNotFoundError: No module called 'src.module_03'I add the file to the
srcfolder and the test passes, I close the fileI add an import statement to
test_module_not_found_error.py10 import src.module_03 11 import src.module_04 12 13 14# Exceptions seenthe terminal shows ModuleNotFoundError
ModuleNotFoundError: No module named 'src.module_04'I add
module_04.pyto thesrcfolder, the test passes and I close the fileI add another import statement to
test_module_not_found_error.py11 import src.module_04 12 import src.module_05 13 14 15# Exceptions seenthe terminal shows ModuleNotFoundError
ModuleNotFoundError: No module named 'src.module_05'I add the file to the
srcfolder, the test passes and I close the fileanother import statement in
test_module_not_found_error.py12 import src.module_05 13 import src.module_06 14 15 16# Exceptions seenthe terminal shows ModuleNotFoundError
ModuleNotFoundError: No module named 'src.module_06'I add
module_06.pyto thesrcfolder, the test passes, and I close the fileI add an import statement in
test_module_not_found_error.py13 import src.module_06 14 import src.module_07 15 16 17# Exceptions seenthe terminal shows ModuleNotFoundError
ModuleNotFoundError: No module named 'src.module_07'I add the module to the
srcfolder, the test passes and I close the fileI add an import statement to
test_module_not_found_error.py14 import src.module_07 15 import src.module_08 16 17 18# Exceptions seenthe test passes
I add
module_08.pyto thesrcfolder, the test passesI add the last import statement to
test_module_not_found_error.py15 import src.module_08 16 import src.module_09 17 18 19# Exceptions seenthe terminal shows ModuleNotFoundError
ModuleNotFoundError: No module named 'src.module_09'
close the project
I close
test_module_not_found_error.pyin the editorI click in the terminal, then use q on the keyboard to leave the tests. The terminal goes back to the command line
I change directory to the parent of
module_not_found_errorcd ..the terminal shows
...\pumping_pythonI am back in the
pumping_pythondirectory
review
I ran a test for ModuleNotFoundError to practice making Python modules
code from the 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