How to run a notebook using command line

That command you quote runs the notebook in the notebook. You should see your print statements ran in the notebook now when you open the notebook. Running it using the command line just triggers running the notebook. The output from inside the notebook doesn’t go to stdout on the command line from where you triggered running it.

If you want magics and sending shell commands via ! to work, why isn’t your bash function the following?

nbrun() { jupyter nbconvert --to script "$1";  mv  "${1%.*}".py run_this.ipy; ipython run_this.ipy}

More details on that suggestion / plus, Jupytext route from notebook to running as Python/IPython

For those following along later who are maybe less bash-oriented, I’m going to break down those steps in my suggested edit of the bash function, and as part of those steps use Jupytext, which I posted about near the start of this thread since it is a nice tool to have in your toolbox. Jupytext does some very related steps and adds some convenience if you want the produced script automatically looking cleaner and closer to a true Python script. (In other words, none of the get_ipython() cruft.)
If you want to run the notebook as a script, then you can use jupytext to convert it to python on command line according to here:

jupytext --to py notebook.ipynb 

Following that then run the script like normal python python <converted_notebook>.py. (Specifically, if you were using the example from jupytext where the notebook is named ‘notebook.ipynb’, the command to run the converted notebook as a script would be python notebook.py.) Then, if you had print statements inside your original notebook, they will go to stdout.

However, @drscotthawley raised the issue of magics and sending shell commands via !. Jupytext and IPython can help you there. See below for how you should be using pip magics and not !pip.) Jupytext includes an option to not comment off the magics and lines beginning with exclamation points, using comment_magics=false. Then you can rename that generated script to be an IPython script by changing the extension to .ipy and run it with ipython instead of python.

So the steps on the command line all together would be:

jupytext --to py notebook.ipynb --opt comment_magics=false
mv notebook.py notebook.ipy
ipython notebook.ipy

Along the line of @drscotthawley 's bash function above, you can substitute in jupyter nbconvert --to script for the Jupytext line.


Best practice for running pip install and conda install in notebooks is now to use the magics. So it should be %pip and not !pip. With conda, the best practice is to now use %conda install in a notebook. See here. Those are IPython magics & so they work with IPython, too.

2 Likes