Select to view content in your preferred language

Broken Magic command

356
1
10-09-2024 11:41 AM
Pukawai
Occasional Contributor

I'm new to using notebooks and have started using functions to avoid copying the same code all over the place. I put all these functions in a notebook called MyFunctons and then used the %run "magic" command to import them into any other notebook where I needed to use them. This worked GREAT! ..... Until it didn't

Somewhere along the line it broke and now I get this error:

 

%run MyFunctions.ipynb   ###Import functions from MyFunctions notebook
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
In  [2]:
Line 1:     %run MyFunctions.ipynb   ###Import functions from MyFunctions notebook

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\IPython\core\interactiveshell.py, in run_line_magic:
Line 2418:  result = fn(*args, **kwargs)

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\decorator.py, in fun:
Line 232:   return caller(func, *(extras + args), **kw)

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\IPython\core\magic.py, in <lambda>:
Line 187:   call = lambda f, *a, **k: f(*a, **k)

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\IPython\core\magics\execution.py, in run:
Line 725:   raise Exception(msg)

Exception: File `'MyFunctions.ipynb.py'` not found.
---------------------------------------------------------------------------

 

 

Note that it started appending ".py" to the notebook file.

I was able to "fix" it by creating a new project, creating new notebooks and copying the code from the broken project/notebook. I don't know if the project got corrupted, or the notebook got corrupted. Searching on the internet it appears that this is a problem others have had elsewhere (not within ArcGIS Pro) but not when it works and then stopped working. It was suggested that a module called import_ipynb is more reliable but it is not among the modules that Pro comes with and I can't use pip to install it.

Does anybody know how this got corrupted and if there is a way to fix it short of rebuilding the whole project?

0 Kudos
1 Reply
HaydenWelch
MVP Regular Contributor

Your error is coming from here:

        except IOError as e: # An IOError is found in your case
            try:
                msg = str(e)
            except UnicodeError:
                msg = e.message
            if os.name == 'nt' and re.match(r"^'.*'$",fpath):
                warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
            raise Exception(msg) # And finally raised here

 

An IOError is raised by this 'file_finder' callable:

def get_py_filename(name):
    """Return a valid python filename in the current directory.

    If the given name is not a file, it adds '.py' and searches again.
    Raises IOError with an informative message if the file isn't found.
    """

    name = os.path.expanduser(name)
    if os.path.isfile(name):
        return name
    if not name.endswith(".py"):
        py_name = name + ".py"
        if os.path.isfile(py_name):
            return py_name
    raise IOError("File `%r` not found." % name)

 

Notice that this function attempts to find missing filed by appending '.py' to the end of the input then passes that back to the 'run' function. So the '.py' you see is a side effect of the 'run' function not finding that 'MyFunctions.ipynb' file in the same directory as the active notebook.

 

You can fix this by manually adding the 'MyFunctions.ipynb' file to the same directory as the active notebook, or putting that file in a central location and using its fullpath:

%run r'C:\path\to\MyFunctions.ipynb'   ###Import functions from MyFunctions notebook
0 Kudos