ModuleNotFoundError: No module named in Python

Common functions in Python are grouped under modules. If the module is not found in python, the error “ModuleNotFoundError: No module named ‘Decimal'” error will be thrown. In this post, what this exception is and how to fix it if it happens.

The modules in the python have one or more functions. Commonly used functions are created and grouped into modules. Some of the modules are part of the python that will come with the installation of the python. They’re all core modules.

Custom modules are created by the user. Commonly used functions are added to these custom modules. These modules are used for all projects used by the user. These modules are company-specific or domain-specific. It helps to re-use the code.

Traceback (most recent call last):
  File "D:\pythonexample.py", line 2, in <module>
    import Decimal
ModuleNotFoundError: No module named 'Decimal'
>>>


Root Cause

Modules are plugged into the code using the import statement. If there is any issue in the import statement, this ModuleNotFoundError error will occur. The python program attempts to link the module specified in the import statement. If the program is unable to link the module for any reason, this error will be shown in the console window.

The reasoncould be that the name of the module is incorrect. Otherwise, the module is not included in the library of python modules. There might be a linking problem with the modules. There are several reasons for this error.



How to reproduce this issue

The easy way to replicate this problem is to import a module that does not exist in the python. In the python program, add a name for the module that is wrong. In the code below, the import statement includes the name of the module as Decimal. Decimal does not exist in the python as a module.

import math
import Decimal
Decimal(math.factorial(171))


Solution 1

In the python program, check the import statement. If the name of the module is wrong, change the name of the module. The correct name of the module will link to the python module. This will correct the error.

import math
from decimal import *
Decimal(math.factorial(171))


Solution 2

If the name of the module in the import statement is right, test the paths linking the python installation and module. Make sure the module is available for installation on the python. In the case of custom module, make sure that all steps are followed.



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *