23

Whenever I try to use any of the built-in functions of Python's exponentiation and logarithms module, I get an error like this:

NameError: name 'sqrt' is not defined

I have tried using math.sqrt(4),sqrt(4) and sqrt(4.0), but none of them work. The exception is pow, which works as it's supposed to. This is really strange and I'm not sure what's wrong.

juliomalegria
  • 24,229
  • 14
  • 73
  • 89
user1126849
  • 291
  • 1
  • 2
  • 8
  • 3
    if the function is not a built-in, you have to import the module it is contained in. See [python library](http://docs.python.org/library/), and the [list of built-ins](http://docs.python.org/library/functions.html). – President James K. Polk Jan 09 '12 at 02:22
  • I'm trying to imagine a scenario where you wouldn't want _math_ to be imported automatically on startup. [Python: Is there a place when I can put default imports for all my modules?](https://stackoverflow.com/q/1350887/86967) – Brent Bradburn Apr 18 '22 at 14:09

7 Answers7

57

pow is built into the language(not part of the math library). The problem is that you haven't imported math.

Try this:

import math
math.sqrt(4)
dave
  • 12,406
  • 10
  • 42
  • 59
18

You can also import as

from math import *

Then you can use any mathematical function without prefixing math. e.g.

sqrt(4)
Softec
  • 1,087
  • 11
  • 14
  • But be aware that the [Style Guide for Python Code](https://peps.python.org/pep-0008/) says: "Wildcard imports (`from import *`) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.". – Matthias Jun 29 '23 at 05:31
5

add:

import math

at beginning. and then use:

math.sqrt(num)  # or any other function you deem neccessary
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Arnab Ghosal
  • 483
  • 1
  • 4
  • 11
2

You need to say math.sqrt when you use it. Or, do from math import sqrt.

Hmm, I just read your question more thoroughly.... How are you importing math? I just tried import math and then math.sqrt which worked perfectly. Are you doing something like import math as m? If so, then you have to prefix the function with m (or whatever name you used after as).

pow is working because there are two versions: an always available version in __builtin__, and another version in math.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
1
import math #imports math module

import math as m
print(m.sqrt(25))

from math import sqrt #imports a method from math module
print(sqrt(25))

from math import sqrt as s
print(s(25))

from math import *
print(sqrt(25))
0

In

from math import sqrt

Using sqrt(4) works perfectly well. You need to only use math.sqrt(4) when you just use "import math".

Abhishek
  • 9
  • 2
0
import math as m
a=int(input("Enter the no"))
print(m.sqrt(a))

from math import sqrt
print(sqrt(25))

from math import sqrt as s
print(s(25))

from math import *
print(sqrt(25))

All works.

Lucas
  • 1,166
  • 2
  • 14
  • 34
  • 2
    Dont put the code directly. Provide explanation. Go through this [link](https://stackoverflow.com/help/how-to-answer) to answer better – Sumanth Shastry Apr 23 '19 at 11:03