I believe the issue is spelled out in the documentation for IPython magic `%run.
It says towards the bottom of that big section:
“There is one special usage for which the text above doesn’t apply: if the filename ends with .ipy[nb], the file is run as ipython script, just as if the commands were written on IPython prompt.”
I think that means in the case of running in Jupyter, then it is run in the current notebook’s namespace and the flags listed prior to that that you could use, such as -p
to run off in the profiler namespace are not an option, although that doesn’t seem to work even with scripts as it sounds, at least to me, as I puzzled over in the past.
Fortunately, this is easily solved other ways using approaches you’d use to run notebooks from the command line. Because it is a very useful and convenient to have in your Jupyter toolbox, I like to use Jupytext these days since the developer added --execute
. (Install it first with conda install conda-forge::jupytext
or pip install jupytext
depending on your package management.)
Then to execute a notebook on the command line you’d run.
jupytext --execute <my_jupyter_notebook.ipynb>
To translate that to running from inside an active notebook, you just add the exclamation point in front of that command to tell Jupyter to run it off in an isolated process:
!jupytext --execute <my_jupyter_notebook.ipynb>
I am going to adapt this to your provided situation below:
Also see:
- How to run a notebook using command line
- python - Can I run Jupyter notebook cells in commandline? - Stack Overflow
- python - How to run an .ipynb Jupyter Notebook from terminal? - Stack Overflow
Incorporated specifically into your situation
Try out what I came up with by clicking here to launch an active Jupyter session with the new nbc.ipynb
that I came up with, along with versions of nbb.ipynb
and nba.ipynb
.
Details
I think the content in this new version of nba.ipynb
, nbc.ipynb
adapts my suggestions to your provided example. In the course of working it out I expanded on your provided code and you can see what I expect to accompany the new nbc.ipynb
in this gist.
Try it out in current Jupyter without log in or signing up for anything:
click here for a session with nbc.ipynb
ready to be run fresh.