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
I open a terminal to run makePythonTdd.sh with
module_not_found_erroras the name of the project./makePythonTdd.sh module_not_found_errorAttention
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 open it 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 the error 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 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_02the 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_03the terminal shows
ModuleNotFoundError: No module called 'src.module_03'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
deactivatethe terminal goes back to the command line,
(.venv)is no longer on the left side.../pumping_python/module_not_found_errorI 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
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