In python3.8, what's the difference between ImportError
and ModuleNotFoundError
? I'm just wondering what the difference is and why they matter.
According to the python docs:
The ImportError is raised when an import statement has trouble successfully importing the specified module. Typically, such a problem is due to an invalid or incorrect path, which will raise a ModuleNotFoundError in Python 3.6 and newer versions.
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.
issubclass
. –
Vanden According to the python docs:
The ImportError is raised when an import statement has trouble successfully importing the specified module. Typically, such a problem is due to an invalid or incorrect path, which will raise a ModuleNotFoundError in Python 3.6 and newer versions.
They both indicate issues with importing modules.
ImportError is a more general exception that can occur when there is an issue importing a module, such as syntax errors within the module, circular imports, or other problems. It can also occur if there's a problem within the module that prevents it from being properly imported.
On the other hand ModuleNotFoundError is raised when Python cannot find the module specified in the import
statement. It can also occur when the module you're trying to import does not exist or cannot be located in the sys.path
or any other paths specified for module search.
ModuleNotFoundError
- because Python has to actually search for the module in these cases, so there's the possibility of it not being found. With relative import, either a specific place to look has already been determined, or else relative import is not possible - therefore, the exception will be of the base ImportError
type. –
Japanese TL;DR You should catch ModuleNotFoundError
to detect when a package is not installed.
ModuleNotFoundError
is only raised when no module is found with that name, usually because the package is not installed. It was introduced in python 3.6+ and is a subclass of ImportError
.
ImportError
is raised when a module fail to import, for one of a million reasons.
The main use case is to detect when an optional library is installed to enable additional features. For example a data science tool can support exporting to an excel file, but only when an excel library is installed (xlsxwriter or openpyxl).
Applications can detect JIT libraries (cython, numba), cryptography libraries, compression libraries (lz4, zstd, snappy, brotli), export formats (csv, excel, openXML).
You should catch ModuleNotFoundError
to detect when a package is not installed.
If you catch ImportError
instead of ModuleNotFoundError
, you might silently catch and discard genuine errors like the module being buggy or not working on that python version or incompatibilities between packages, which you usually want to see and fix.
Here is a test to show one difference:
# mylibrary.py
def hello():
print("hello")
# myapp.py
try:
from mylibrary import missingvar
except ImportError:
print("caught ImportError")
try:
from mylibrary import missingvar
except ModuleNotFoundError:
print("caught ModuleNotFoundError")
output:
$ python3 myapp.py
caught ImportError
Traceback (most recent call last):
File "/home/user/myapp.py", line 7, in <module>
from mylibrary import missingvar
ImportError: cannot import name 'missingvar' from 'mylibrary' (/home/user/mylibrary.py)
© 2022 - 2024 — McMap. All rights reserved.
ModuleNotFoundError
is a specific type ofImportError
. See exception hierarchy. So if you have anexcept ImportError
block, that will coverModuleNotFoundError
. – Gerbold