pythonshellipythonpython-importipython-magic

Import functions that use ipython magic


In ipython cells you can execute shell commands as follows:

ipython:

print("asdf")
!echo asdf

However, if you try to import this code from file,

asdf.py:

def asdf():
  print("asdf")
  !echo asdf

ipython:

from asdf import asdf

it results in an error.

!echo asdf
^
SyntaxError: invalid syntax

The use case for that is repetative massive scripts in google colab utilizing ffmpeg, wget, mount, e.t.c.
While you can use os.system or subprocess, they are not as interactive in providing real-time stdout.


Solution

  • Use case solution

    If you want to reuse code that has ipython magic functions in it, simply use %run instead of the import.

    ipython:

    %run utils.ipynb
    

    utils.ipynb:

    def asdf():
      print("asdf")
      !echo asdf
    

    Example above works fine.
    Taken from this answer.


    On reusing ipython code

    Why the original question was an A-B problem of some kind? The reason is that you should not really drag ipython additional functionality to the pure python execution.

    Sure, there are ways to import .ipynb files to python code: Jupyter notebook's Notebook Loader, ipynb package, nbimporter package (but even the nbimporter's author says you should not really do that. Just convert your notebook to .py using VSCode or another tool).

    So, if you use ipython research environment and features, just stick with it. Or switch to pure python environment with proper modules, testing, e.t.c.


    Also, if you're struggling with doing wget:

    file_id = "1xE8Db1zvQ7v-z13CO2qc3F6wJmtr3YHu" # can be extracted from public link
    file_out_name = "utils.ipynb"
    !wget "https://docs.google.com/uc?export=download&id=""$file_id" -O "$file_out_name"
    

    However, on the problem of calling cmd, I see no way of executing shell commands interactively in python with one line. While subprocess can do that, it takes multiple lines to achieve.