从 IPython 执行 python 文件
Executing a python file from IPython
我对 NumPy/SciPy 和 IPython 比较陌生。
要在 python 交互模式下执行 python 脚本,我们可以使用以下命令。
>>> import os
>>> os.system('executable.py')
然后可以从python提示符中看到打印输出。
但同样的想法不适用于 IPython 笔记本。
In [64]:
import os
os.system('executable.py')
Out[64]:
0
在这种情况下,我看不到任何打印输出。 notebook 只告诉天气执行是否成功。当我使用 IPython notebook 时,有什么方法可以查看输出吗?
使用魔法函数%run
:
%run executable.py
这会正确地将 stdout
重定向到浏览器,您将在笔记本中看到程序的输出。
它为您提供了来自命令行的 运行 的典型特征以及 Python 回溯(如果有异常)。
Parameters after the filename are passed as command-line arguments to
the program (put in sys.argv). Then, control returns to IPython's
prompt.
This is similar to running at a system prompt python file args
,
but with the advantage of giving you IPython's tracebacks, and of
loading all variables into your interactive namespace for further use
(unless -p is used, see below).
选项 -t
乘以您的脚本。使用 -d
它在调试器 pdb 中运行。探索更多不错的选择。
我对 NumPy/SciPy 和 IPython 比较陌生。
要在 python 交互模式下执行 python 脚本,我们可以使用以下命令。
>>> import os
>>> os.system('executable.py')
然后可以从python提示符中看到打印输出。
但同样的想法不适用于 IPython 笔记本。
In [64]:
import os
os.system('executable.py')
Out[64]:
0
在这种情况下,我看不到任何打印输出。 notebook 只告诉天气执行是否成功。当我使用 IPython notebook 时,有什么方法可以查看输出吗?
使用魔法函数%run
:
%run executable.py
这会正确地将 stdout
重定向到浏览器,您将在笔记本中看到程序的输出。
它为您提供了来自命令行的 运行 的典型特征以及 Python 回溯(如果有异常)。
Parameters after the filename are passed as command-line arguments to the program (put in sys.argv). Then, control returns to IPython's prompt.
This is similar to running at a system prompt
python file args
, but with the advantage of giving you IPython's tracebacks, and of loading all variables into your interactive namespace for further use (unless -p is used, see below).
选项 -t
乘以您的脚本。使用 -d
它在调试器 pdb 中运行。探索更多不错的选择。