1

Is it possible to run a script that uses ipython-specific functionality (non-python)?

I am considering ipython as an extension of python here, since it supports things like bang (!) and magic (%, %%) commands.

Therefore my question is that if it's possible to have something like an .ipy script file that is written in the "ipython language" and that I can run non-interactively, like I would do with a normal python script (python script.py).

A simple example of what I would like to achieve:

# valid ipython code
files = !ls
print(files)

(that is my ls.ipy)

run it like:

python ls.ipy
# output: list of files in this directory
Michele Piccolini
  • 2,634
  • 16
  • 29
  • 1
    https://stackoverflow.com/q/10361206/7954504 may be helpful – Brad Solomon Jun 04 '20 at 12:50
  • thanks! I had a look there an at [jupyter's nbconvert](https://nbconvert.readthedocs.io/en/latest/usage.html). Unfortunately in the question you linked all solutions write in python (not ipython as I am intending it here) and import the `IPython` library and make use of it. I think I found a way just now, playing around a bit. – Michele Piccolini Jun 04 '20 at 12:55

2 Answers2

0

Run this in your shell:

ipython ls.ipy
Balaji Ambresh
  • 4,977
  • 2
  • 5
  • 17
  • Ops, just found out I can do that by trial and error and self-answered. Is this documented in the ipython documentation? (I'm sure it is but I didn't find it) – Michele Piccolini Jun 04 '20 at 13:00
  • `ipython --help` provides the usage with file as an argument. – Balaji Ambresh Jun 04 '20 at 13:05
  • Thanks! But do you know if it's well documented somewhere in the [official documentation](https://ipython.readthedocs.io/en/stable/index.html). I think this feature ("ipython as a scripting language") is very useful (it makes me want to use this instead of bash scripts on my system) and should be more visible. (Although I recognize that the whole point of "I"python is the "I"nteractivity. But still, I'm pretty excited about this alternative usage!) – Michele Piccolini Jun 04 '20 at 13:20
  • I don't see it either. You can improve the documentation [here](https://github.com/ipython/ipython/blob/2922831acf6d7c185fb863b4436ffd7cfa665675/docs/source/interactive/tutorial.rst) – Balaji Ambresh Jun 04 '20 at 13:25
0

Playing around a bit I just found a way:

ipython myscript.ipy

with myscript.ipy written in "ipython" (it's not a jupyter notebook, it's not a python script that imports IPython, but it's a python + bang ! and magic % syntax). For example:

files = !ls
print(files)

or

a = 1
%whos
Michele Piccolini
  • 2,634
  • 16
  • 29