Question

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
Was it helpful?

Solution 2

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!

OTHER TIPS

more a comment than an answer (but i cant put code properly in a comment): does it work if you put the next code in one 'something.py' file, and than just do python something.py in your terminal.

import numpy
def sine_test(theta):
    return numpy.sin(theta)
u=sine_test(1)

Just to see if it is something related to a bad install, or if it is something related to ipython, or if it is something to do with how your files interact with each other. This way the problem can be narrowed down a bit. If this works as it should, than it is related to how your files work with each other or to your ipython. The next step would than be to just test it in different files like you do, but not in ipython, but just in terminal to see if it works in python, and if it is not a ipython problem. Than if that works too, than it is probably related to your ipython.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top