1

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
1
  • I know about runipy, I'm interested in ipython as a scripting language. Commented May 25, 2016 at 18:11

2 Answers 2

2
+100

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, .ipy is what I was looking for. Using Py3 whenever 3rd party library exists for the job, so Py2 for bigger projects still.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.