Python 3.6+Exceptionsexception
ModuleNotFoundError
built-in exceptions
A subclass of ImportError. Raised when the module is not found in sys.path at all. Python 3.6+.
Syntax
raise ModuleNotFoundError('No module named ...')
Usually the library is not missing at all — it is just installed for a different interpreter than the one running your code: another venv, another pip, IDE versus terminal. You rarely need to catch ModuleNotFoundError by name, since it is a subclass of ImportError and except ImportError already covers it. Separate them only when it matters whether the package is absent entirely or present but missing the name you asked for — the latter is a plain ImportError.
Examples
import nonexistent_lib # ModuleNotFoundError
try:
import pandas
except ModuleNotFoundError:
print('установи: pip install pandas')
See also
Open in the full glossarydocs.python.orgReport an inaccuracy