Solve Python ModuleNotFoundError

Last Updated On Thursday 2nd Jun 2022

PYTHONPATH – ModuleNotFoundError: No module named

if you get an error like ModuleNotFoundError: No module named, provide the path to the tests:

	python -m pytest tests/
	

print messages to terminal / console

	pytest -s
	

ModuleNotFoundError

This error extends the Python Exception class.

If, for any reason, a submodule is unable to access a reference to one of its dependencies, then it shall throw a ModuleNotFoundError, passing in a reference to itself and a string representation of the dependency.

	Type "help", "copyright", "credits" or "license" for more information.
>>> import maths
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'maths'
>>>
	

In the example above, I added an extra s to math deliberately and ModuleNotFoundError was raised. Lets fix it by removing the extra s from math.

	Type "help", "copyright", "credits" or "license" for more information.
>>> import maths
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'maths'
>>> import math
>>>
	

We fixed it, so let’s use some of the functions from the math module.