Run multiple .py files in a jupyter notebook

Question:

Basically, I get some python files in a same directory of my main jupyter program, and inside this main program, I need a cell that will run all the other python files.

*obs.: the py files are generated dynamically

I’ve tried to use something like

%run *.py

or something like:

while True: %run "script1.py"

but I need to set the name of the script dynamically

Asked By: Pedro Strapasson

||

Answers:

This will run every .py script file in the current directory where the notebook is run, even those script files that the subsequent scripts make in the same directory:

import os
import fnmatch
import glob

executed_scripts = []
extension_to_match = ".py"

def execute_script(s):
    %run {s}

while set(executed_scripts) != set(glob.glob(f"*{extension_to_match}")):
    for file in os.listdir('.'):
        if fnmatch.fnmatch(file, '*'+extension_to_match):
            if file not in executed_scripts:
                execute_script(file)
                executed_scripts.append(file)

I tried to make this universal by not relying on any special packages that allow handling shell wildcards.

You can see it demonstrated in action at the bottom of this notebook. The initial cells in that notebook set things up by making two scripts that each spawn three more scripts each. Each script is run once.

The header of the notebook contains instructions on how you can go here and press launch binder to get a session served by MyBinder.org where that notebook will work. Once the session launches, you can type !curl -OL https://gist.githubusercontent.com/fomightez/bcbeaa34f258e6ab959c842438077cb8/raw/4aa21f205c663688ef552acc38293d574c9667c4/for_so75075576.ipynb in a notebook cell to fetch the notebook in sessions there and then open it and run it actively.

Answered By: Wayne