IPython%run magic-n开关不工作

IPython%run magic-n开关不工作,python,ipython,jupyter-notebook,ipython-magic,Python,Ipython,Jupyter Notebook,Ipython Magic,我在另一个(家长)笔记本中使用%run魔法运行IPython笔记本 如果使用%run调用,我想在子笔记本中隐藏一些输出,我想可以通过测试if\uuuuuuu name\uuuuuuu='\uuuuuu main\uuuu' IPython文档说明,当使用%run-n开关时: \uuuu name\uuuu未设置为\uuuu main\uuuu,而是设置为运行文件的名称,但不带扩展名(就像python在导入下所做的那样)。这允许运行 脚本并重新加载其中的定义,而不调用代码 受if子句保护 然而,它

我在另一个(家长)笔记本中使用
%run
魔法运行IPython笔记本

如果使用
%run
调用,我想在子笔记本中隐藏一些输出,我想可以通过测试
if\uuuuuuu name\uuuuuuu='\uuuuuu main\uuuu'

IPython文档说明,当使用
%run-n
开关时:

\uuuu name\uuuu
未设置为
\uuuu main\uuuu
,而是设置为运行文件的名称,但不带扩展名(就像python在导入下所做的那样)。这允许运行 脚本并重新加载其中的定义,而不调用代码 受
if
子句保护

然而,它似乎对我不起作用。我试过这个:

sub_notebook.ipynb
中:

print(__name__)
%run -n sub_notebook.ipynb
try:
    __file__
    print('I am in an imported notebook')

except NameError:
    print('I am not in an imported notebook')
def has_parent():
    """Return True if this notebook is being run by calling
    %run in another notebook, False otherwise."""
    try:
        __file__
        # __file__ has been defined, so this notebook is 
        # being run in a parent notebook
        return True

    except NameError:
        # __file__ has not been defined, so this notebook is 
        # not being run in a parent notebook
        return False
parent\u notebook.ipynb
中:

print(__name__)
%run -n sub_notebook.ipynb
try:
    __file__
    print('I am in an imported notebook')

except NameError:
    print('I am not in an imported notebook')
def has_parent():
    """Return True if this notebook is being run by calling
    %run in another notebook, False otherwise."""
    try:
        __file__
        # __file__ has been defined, so this notebook is 
        # being run in a parent notebook
        return True

    except NameError:
        # __file__ has not been defined, so this notebook is 
        # not being run in a parent notebook
        return False
这会打印
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

请告诉我如何在
sub_notebook.ipynb
中有选择地运行代码,这取决于它是单独运行还是使用
%run

我正在运行iPython6.1.0版

设置
\uuuu file\uuuu
变量,以便我们可以对此进行测试

我们可以在
sub_notebook.ipynb

print(__name__)
%run -n sub_notebook.ipynb
try:
    __file__
    print('I am in an imported notebook')

except NameError:
    print('I am not in an imported notebook')
def has_parent():
    """Return True if this notebook is being run by calling
    %run in another notebook, False otherwise."""
    try:
        __file__
        # __file__ has been defined, so this notebook is 
        # being run in a parent notebook
        return True

    except NameError:
        # __file__ has not been defined, so this notebook is 
        # not being run in a parent notebook
        return False
单独运行,打印出
我不在导入的笔记本中

我们可以创建一个父笔记本
父笔记本.ipynb
包含:

%run sub_notebook.ipynb
正确运行此命令将打印出导入的笔记本中的

我们可以在
sub_notebook.ipynb
中编写一个简单的测试:

print(__name__)
%run -n sub_notebook.ipynb
try:
    __file__
    print('I am in an imported notebook')

except NameError:
    print('I am not in an imported notebook')
def has_parent():
    """Return True if this notebook is being run by calling
    %run in another notebook, False otherwise."""
    try:
        __file__
        # __file__ has been defined, so this notebook is 
        # being run in a parent notebook
        return True

    except NameError:
        # __file__ has not been defined, so this notebook is 
        # not being run in a parent notebook
        return False
然后可以保护不应在父笔记本中打印的代码:

if not has_parent():
    print('This will not print in the parent notebook')

快速查看
%run
的源代码可以发现,如果文件名以
.ipy
.ipynb
结尾,则该函数甚至在检查
-n
开关之前都会返回。我创造了。