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

4

24

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

Supremacist answered 11/7, 2020 at 21:25 Comment(2)
ModuleNotFoundError is a specific type of ImportError. See exception hierarchy. So if you have an except ImportError block, that will cover ModuleNotFoundError.Gerbold
docs.python.org/3/library/exceptions.html#ImportErrorRodneyrodolfo
D
7

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.

Daedalus answered 11/7, 2020 at 21:26 Comment(0)
F
18

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.

Fenella answered 11/7, 2020 at 21:31 Comment(1)
Plus 1 for introducing me to issubclass.Vanden
D
7

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.

Daedalus answered 11/7, 2020 at 21:26 Comment(0)
H
3

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.

Heirship answered 25/10, 2023 at 17:45 Comment(1)
Note that only absolute imports will cause 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
J
1

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)
Janettjanetta answered 19/10, 2023 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.