%run works only once in Jupyter Notebook

I have two Jupyter notebooks open in the same folder: functions.ipynb and preprocessing.ipynb.
In functions.ipynb I have defined a series of functions that I will call in the other one.

In the first cell of preprocessing.ipynb I execute

%run 'functions.ipynb'

and it works just fine. But if I run once again the same cell I get this error:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/IPython/core/magics/execution.py in run(self, parameter_s, runner, file_finder)
    696             fpath = arg_lst[0]
--> 697             filename = file_finder(fpath)
    698         except IndexError:

~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/IPython/utils/path.py in get_py_filename(name, force_win32)
    108     else:
--> 109         raise IOError('File `%r` not found.' % name)
    110 

OSError: File `'functions.ipynb.py'` not found.

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
<ipython-input-2-330907dc0fe2> in <module>
----> 1 get_ipython().run_line_magic('run', "'functions.ipynb'")

~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2324                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2325             with self.builtin_trap:
-> 2326                 result = fn(*args, **kwargs)
   2327             return result
   2328 

<decorator-gen-59> in run(self, parameter_s, runner, file_finder)

~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

~/anaconda3/envs/tensorflow2_p36/lib/python3.6/site-packages/IPython/core/magics/execution.py in run(self, parameter_s, runner, file_finder)
    706             if os.name == 'nt' and re.match(r"^'.*'$",fpath):
    707                 warn('For Windows, use double quotes to wrap a filename: %run "mypathmyfile.py"')
--> 708             raise Exception(msg)
    709         except TypeError:
    710             if fpath in sys.meta_path:

Exception: File `'functions.ipynb.py'` not found.

I really can’t understand why this code runs only once.
I am using Python 3.6 in Amazon SageMaker

Source: Python-3x Questions

LEAVE A COMMENT