I'm running an IPython notebook by using the %run
magic in another (parent) notebook.
I would like to hide some of the output in the child notebook if it is called using %run
and think I can do this by testing for if __name__ == '__main__'
The IPython documentation says that, when %run -n
switch is used:
__name__
is NOT set to__main__
, but to the running file's name without extension (as python does under import). This allows running scripts and reloading the definitions in them without calling code protected by anif __name__ == "__main__"
clause.
However, it does not seem to be workign for me. I tried this:
In sub_notebook.ipynb
:
print(__name__)
In parent_notebook.ipynb
:
%run -n sub_notebook.ipynb
This prints __main__
but the documentation says that it should have printed sub_notebook
.
Please could you let me know how I can selectively run code in sub_notebook.ipynb
depending on whether it is being run alone or using %run
?
I'm running IPython version 6.1.0