Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3.8: What's the difference between ImportError and ModuleNotFoundError

In python3.8, what's the difference between ImportError and ModuleNotFoundError? I'm just wondering what the difference is and why they matter.

like image 656
theX Avatar asked Sep 06 '25 04:09

theX


1 Answers

ModuleNotFoundError is a kind of ImportError:

>>> issubclass(ModuleNotFoundError, ImportError)
True

It's raised specifically when the module cannot be found at all. Other problems can occur after the file is found, but during the actual process of loading the file or defining the function: those would raise ImportError.

There's probably not much you can do about a ModuleNotFoundError; you can either ignore it and not use the module you tried to import later in the code, or exit and fix your environment so that the module will be found.

like image 130
chepner Avatar answered Sep 08 '25 18:09

chepner