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.
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".
You can also import as
Then you can use any mathematical function without prefixing math. e.g.
pow
is built into the language(not part of the math library). The problem is that you haven't imported math.Try this:
You need to say
math.sqrt
when you use it. Or, dofrom math import sqrt
.Hmm, I just read your question more thoroughly.... How are you importing
math
? I just triedimport math
and thenmath.sqrt
which worked perfectly. Are you doing something likeimport math as m
? If so, then you have to prefix the function withm
(or whatever name you used afteras
).pow
is working because there are two versions: an always available version in__builtin__
, and another version inmath
.add:
at beginning. and then use: