简体   繁体   English

从IPython运行组合的Shell脚本(非交互式)

[英]Running combined shell script from IPython (non-interactively)

Is there a possibility to run extended IPython (which can be used in interactive console, see example) non-interactively as a script? 是否有可能以脚本非交互方式运行扩展的 IPython(可在交互式控制台中使用,请参见示例)? I haven't found it documented anywhere. 我没有在任何地方记录它。

Example: 例:

#!/magical/ipython/command --that-i-want
from __future__ import print_function
d = !date +%s
!echo $d.s $(($d.s / 1000))

files = !ls -A
for i, f in enumerate(files):
    print("File number %s is '%s'" % (i, f))

Expected output should be: (result of copy&pasting to IPython interactive console) 预期输出应为:(复制并粘贴到IPython交互式控制台的结果)

1463917269 1463917
File number 0 is '.ipynb_checkpoints'
File number 1 is 'file1.txt'
File number 2 is 'file2.txt'

When shebang is ipython it fails with: 当shebang是ipython它失败并显示:

d = !date +%s
    ^
SyntaxError: invalid syntax

You can run this file. 您可以运行此文件。

vi shebang.py

get_ipython().run_cell("""
from __future__ import print_function
d = !date +%s
!echo $d.s $(($d.s / 1000))

files = !ls -A
for i, f in enumerate(files):
    print("File number %s is '%s'" % (i, f))
""")

and

ipython shebang.py

name your file with an .ipy extension, and IPython will be able to run it with (most) of its syntax extensions. 使用.ipy扩展名命名文件,然后IPython将能够使用(大多数)语法扩展名运行该文件。

Though everything IPython does is just syntactic sugar, you can do it in pure Python. 尽管IPython所做的只是语法糖,但是您可以在纯Python中完成它。

(Please use Python 3 also) (也请使用Python 3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM