Is there a possibility to run extended IPython (which can be used in interactive console, see example) non-interactively as a script? 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)
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:
d = !date +%s
^
SyntaxError: invalid syntax
2 Answers 2
name your file with an .ipy extension, and IPython will be able to run it with (most) of its syntax extensions.
Though everything IPython does is just syntactic sugar, you can do it in pure Python.
(Please use Python 3 also)
1 Comment
.ipy is what I was looking for. Using Py3 whenever 3rd party library exists for the job, so Py2 for bigger projects still.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
runipy, I'm interested in ipython as a scripting language.