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
Just M
28k6 gold badges85 silver badges75 bronze badges
asked May 22, 2016 at 11:45
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)

answered May 29, 2016 at 18:51
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

Just M
28k6 gold badges85 silver badges75 bronze badges
answered May 29, 2016 at 13:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.