在python函数中使用numpy中的函数

在python函数中使用numpy中的函数,python,python-2.7,numpy,enthought,Python,Python 2.7,Numpy,Enthought,我想在Python中定义一个函数,它本身使用numpy中的函数,即正弦函数“sin”。为了简化问题,我制作了一个测试函数“sine_test.py”,如下所示: import numpy def sine_test(theta): return numpy.sin(theta) import numpy from sine_test import sine_test u=sine_test(1) 我还想从名为“use_sine_test.py”并保存在同一目录中的脚本运行此函数,其内

我想在Python中定义一个函数,它本身使用numpy中的函数,即正弦函数“sin”。为了简化问题,我制作了一个测试函数“sine_test.py”,如下所示:

import numpy
def sine_test(theta):
    return numpy.sin(theta)
import numpy
from sine_test import sine_test
u=sine_test(1)
我还想从名为“use_sine_test.py”并保存在同一目录中的脚本运行此函数,其内容如下:

import numpy
def sine_test(theta):
    return numpy.sin(theta)
import numpy
from sine_test import sine_test
u=sine_test(1)
我希望它将u定义为1弧度的正弦,大约0.84。“use_sine_test.py”和“sine_test.py”都在同一个文件夹中(D:/Python)。但是,如果使用run按钮运行“use_sine_test.py”Python脚本,则会出现如下所示的“NameError:global name'sin'is not defined”错误

你知道这是什么原因吗

致以最良好的祝愿, 库尔特

%rund:/Python/use\u sine\u test.py
---------------------------------------------------------------------------
NameError回溯(最近一次呼叫上次)
C:\Program Files\Enthught Python 7.2.2\lib\site packages\IPython\utils\py3compat.py在execfile(fname、glob、loc)中
166.其他:
167 filename=fname
-->168 exec编译(脚本文本,文件名,'exec'),在glob,loc中
169其他:
170 def execfile(fname,*其中):
D:\Python\在()中使用\u sine\u test.py
1进口纽米
2从正弦测试导入正弦测试
---->3 u=正弦试验(1)
D:\Python\sine\u test.py in sine\u test(θ)
1进口纽米
---->2 def正弦测试(θ):
3返回numpy.sin(θ)
NameError:未定义全局名称“sin”

与其说是答案,不如说是评论(但我不能将代码正确地放入评论中): 如果您将下一个代码放在一个'something.py'文件中,而不只是在终端中执行python something.py,那么它是否有效

import numpy
def sine_test(theta):
    return numpy.sin(theta)
u=sine_test(1)
只是想看看它是否与错误安装有关,或者是否与ipython有关,或者是否与文件之间的交互有关。这样问题就可以缩小一点。如果这是应该的,那么它与文件之间的工作方式或ipython有关。下一步是像您一样在不同的文件中测试它,但不是在ipython中,而是在terminal中,以查看它是否在python中工作,以及它是否不是ipython问题。如果这也行得通,那么它可能与您的ipython有关。

所有

在BrenBarn的评论之后,我尝试在Canopy中重新启动代码编辑器,现在它似乎可以工作了:

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

谢谢你的评论

您是否尝试过重新启动Python(或者IPython,因为它看起来就是您正在使用的)?你确定用粘贴的代码保存了文件吗?错误消息与您发布的代码不一致,如果没有以某种方式重新加载文件,有时会发生这种情况。
Python 7.2.2
?你怎么弄到的?更严重的是,您是否直接从实际的解释器会话复制/粘贴了此输出?谢谢您的回复,请使用DeathStar。我试着运行以下代码:import numpy from sine_test import sine_test u=sine_test(1)def sine_test(θ):返回sin(θ),换句话说,函数定义和脚本在一个“.py”中运行,但我仍然得到相同的错误消息(“全局名称'sin'未定义”)。@khpeek你能说说你在这里的评论吗,但是把它作为你原来问题的编辑?这样其他人也能看到?