如何将Python脚本发送到IPython或在IPython中运行Python脚本而不导入numpy

she*_*per 3 python vim ipython

我有一个包含使用numpy函数的脚本的python文件.

我认为IPython已经加载了numpy,所以当我这样做时,我没有在文件中导入numpy:

%run my_python_file.py 
Run Code Online (Sandbox Code Playgroud)

它由于未知的功能而失败numpy.

所以,我的问题是:

有没有办法在已经由IPython导入的模块的Python文件中运行脚本?

非常感谢!

dae*_*les 5

如果你在numpy -pylab交互式shell中,请使用

run -i script.py
Run Code Online (Sandbox Code Playgroud)

对于你所要求的.它具有在python脚本中保留(或覆盖)任何全局变量的副作用.基本上,ipython就像你手动输入文件一样.

也许最有用的原因是你也可以引用在ipython会话中创建的其他变量,例如,在脚本运行之间手动更改它们.

对于一个简单的例子,如果是script.py

AA = np.array([[1,2],[3, myVar]])
print AA
Run Code Online (Sandbox Code Playgroud)

然后在ipython你可以

In [1]: myVar = 7
In [2]: run -i script.py
[[1 2]
 [3 7]]
In [3]: myVar = 17
In [4]: run -i script.py
[[ 1  2]
 [ 3 17]]
In [5]: 
Run Code Online (Sandbox Code Playgroud)