I'd like to define a function in Python which itself uses a function from numpy, namely, the sine function 'sin'. To simplify the problem, I've made a test function "sine_test.py" as follows:
import numpy
def sine_test(theta):
return numpy.sin(theta)
I also want to run this function from a script, named "use_sine_test.py" and saved in the same directory, which reads as follows:
import numpy
from sine_test import sine_test
u=sine_test(1)
I would expect this to define u as the sine of 1 radian, about 0.84. Both "use_sine_test.py" and "sine_test.py" are in the same folder (D:/Python). However, if I run the "use_sine_test.py" Python script using the run button, I get a "NameError: global name 'sin' is not defined" error as shown below.
Any ideas what might be causing this?
Best regards, Kurt
%run D:/Python/use_sine_test.py
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
C:\Program Files\Enthought Python 7.2.2\lib\site-packages\IPython\utils\py3compat.py in execfile(fname, glob, loc)
166 else:
167 filename = fname
--> 168 exec compile(scripttext, filename, 'exec') in glob, loc
169 else:
170 def execfile(fname, *where):
D:\Python\use_sine_test.py in <module>()
1 import numpy
2 from sine_test import sine_test
----> 3 u=sine_test(1)
D:\Python\sine_test.py in sine_test(theta)
1 import numpy
----> 2 def sine_test(theta):
3 return numpy.sin(theta)
NameError: global name 'sin' is not defined
All,
Following BrenBarn's comment, I've tried to restart the Code Editor in Canopy and it seems to work now:
Welcome to EPD's interactive data-analysis environment!
with pylab-backend: wx
Type '?' for more information.
Welcome to pylab, a matplotlib-based Python environment [backend: WXAgg].
For more information, type 'help(pylab)'.
%run D:/Python/use_sine_test.py
u
Out[2]: 0.8414709848078965
Thanks for your comments!