NameError: name ‘math’ is not defined

In python, NameError: name is not defined error is shown due to the name is not available in python. name is not defined error shows the name in ” is not defined in python. The math operations are performed at Python using the math module. The “NameError: Name ‘math’ is not specified” error is common if you don’t import the “math” module into the program. We’ll see the exception in this post “NameError: Name ‘ math ‘ is not specified.”

Common libraries are structured in python as modules. The “math” module contains all mathematical related functions. If you are using the math functions in the python program you may import the math module. If it is not imported correctly the error will be thrown. The name error indicates that the python progrom does not identify the word “math” in the program.

Traceback (most recent call last):
  File "D:\pythonexample.py", line 1, in <module>
    p = math.pow(3,1)
NameError: name 'math' is not defined
>>>


Root Cause

The root cause of the error is “math” is not defined in the program. The python program detects the word “math” within the program but can not understand what the word “math” is. The word could be the name of the variable, the name of the class, the name of the function or the name of the module.

The program checks the name of the word to match any of the variables defined. If the name is not matched to any of the above, it will be thrown as the name defined is not identified by the program. The program expects to qualify the name for any of the known python structures.

In python, “math” is the name of a module which is responsible for all specific math functions. The program does not recognize the math module as it is not being imported.



How to reproduce this issue

Use any mathematical operation in the python program without importing a math module. This is explained in the program below. The program is not going to understand the word “math.” Therefore, this error is being thrown

p = math.pow(3,1)

Output

Traceback (most recent call last):
  File "D:\pythonexample.py", line 1, in <module>
    p = math.pow(3,1)
NameError: name 'math' is not defined
>>>


Solution 1

The “math” module is missing from the above program. Before calling the power function in python, import the “math” module. The import statement will link the math module to the program. The pow function is available in the math module and can be invoked from the program.

import math
p = math.pow(3,2)
print ("power" , p)

Output

power 9.0
>>>


Solution 2

There is another way to import the modules into the program. If the program requires a single function from the module, the function can be imported. The function can be imported using the keyword “from”. The import statement is “Math Import Pow.” In this import statement, math is the name of the module, and pow is the name of the function. The rest of the functions in the math module are not imported into the program.

In the code below, the pow function is imported from the math module.

from math import pow
p = pow(3,2)
print ("power" , p)

Output

power 9.0
>>>



Related Articles

Leave a Reply

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