Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash cell magic in IPython script

Tags:

bash

ipython

This is my first post on SO, so please let me know if the problem is not well defined. I have a script process.ipy in which I am trying to implement a cell magic as follows,

#!/usr/bin/env ipython

%%bash
ls

When I run this on the command line (Ubuntu and zsh shell), I get the following error,

$ ipython process.ipy                                                                                                 
File "<ipython-input-1-f108be8d32f2>", line 3
  %%bash
  ^
SyntaxError: invalid syntax

However, I can run this in the ipython session without a problem,

In [1]: %%bash
   ...: ls
   ...: 
process.ipy

More confusing, is that the single line version works in the script,

#!/usr/bin/env ipython

!ls

What am I doing wrong? Is something not setup correctly?

like image 721
pacificprince Avatar asked Jul 18 '26 22:07

pacificprince


1 Answers

You do not need the #!/usr/bin/env ipython line at the top. The error :

%%bash
ls
like image 188
magic-sudo Avatar answered Jul 20 '26 17:07

magic-sudo