[Numpy-discussion] cos -- NameError: global name 'cos' is not defined

josef.pktd@gmai... josef.pktd@gmai...
Mon Dec 21 17:31:18 CST 2009

On Mon, Dec 21, 2009 at 6:11 PM, Wayne Watson
<sierra_mtnview@sbcglobal.net> wrote:
> Yes, thanks. That's the what I finally changed to. This originated up a
> thread or so when I displayed the highly populated code with math. Some
> said I didn't need it, so I thought I'd give it a go.
> I just started plugging away again with IDLE and am pretty convinced
> that IDLE is something of an enemy. I started afresh loading this into
> the editor:
>    import math
>    print "hello, math world."
>    print math.cos(0.5)
>    print math.sin(0.8)
> Run works fine. No errors. Now I do:
>  >>> dir()
> ['__builtins__', '__doc__', '__file__', '__name__', 'idlelib', 'math']
>  >>>
> OK, swell. Now I import via the script window
>  >>> import numpy as np
>  >>> dir()
> ['__builtins__', '__doc__', '__file__', '__name__', 'idlelib', 'math', 'np']
> I think I'm adding to the namespace both the program the script sees.,
> because adding this ref to np in the program works fine.
>    import math
>    print "hello, math world."
>    print math.cos(0.5)
>    print math.sin(0.8)
>    print np.sin(2.2)
> I've been assuming that IDLE clears the namespace.  It's quite possible
> that I get anomalous results as I move between Run the program via the
> editor, and fiddling in script land. I would like to think that IDLE has
> some way to clear the namespace before it runs the program. If not, yikes!

idle has two modes depending on whether it is started with or without
-n option on the command line. I usually pick this option depending on
what I am doing. From the IDLE help:

Running without a subprocess:

	If IDLE is started with the -n command line switch it will run in a
	single process and will not create the subprocess which runs the RPC
	Python execution server.  This can be useful if Python cannot create
	the subprocess or the RPC socket interface on your platform.  However,
	in this mode user code is not isolated from IDLE itself.  Also, the
	environment is not restarted when Run/Run Module (F5) is selected.  If
	your code has been modified, you must reload() the affected modules and
	re-import any specific items (e.g. from foo import baz) if the changes
	are to take effect.  For these reasons, it is preferable to run IDLE
	with the default subprocess if at all possible.


> Robert Kern wrote:
>> On Mon, Dec 21, 2009 at 11:40, Wayne Watson
>> <sierra_mtnview@sbcglobal.net> wrote:
>>> In this code,
>>> ===========start
>>> import math
>>> import numpy as np
>>> from numpy import matrix
>>> def sinD(D): # given in degrees, convert to radians
>>>    return sin(radians(D))
>>> def cosD(D):
>>>    return cos(radians(D))   <<--------------
>>> def acosD(D):
>>>    acos(radians(D))
>>>    return=====end
>>> the << line produces, "NameError: global name 'cos' is not defined", but
>>> the sin() above it does not? They are both built-in functions.
>> No, they aren't. They are in the math module. You want math.cos(). The
>> same goes for radians() and acos() and sin().
> --
>           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
>             (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
>              Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet
>             "... humans'innate skills with numbers isn't much
>              better than that of rats and dolphins."
>                       -- Stanislas Dehaene, neurosurgeon
>                    Web Page: <www.speckledwithstars.net/>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list