简体   繁体   English

在 jupyter notebook 中使用 %run 而不影响交互式命名空间

[英]Using %run in jupyter notebook without affecting interactive namespace

I am trying to run a python script in a jupyter notebook without the variables in the script being passed to my interactive namespace after the script has been called.我正在尝试在 jupyter notebook 中运行 python 脚本,而脚本中的变量在脚本被调用后没有传递到我的交互式命名空间。

An example below:下面是一个例子:

myscript.py:我的脚本.py:

var1 = 10

My notebook:我的笔记本:

var1 = 9
%run myscript.py
print(var1)

I want the printed answer to be 9 not 10. I have also tried running %run -p myscript.py as stated in the documentation but seem to get the same result.我希望打印的答案是 9 而不是 10。我也尝试过按照文档中的说明运行%run -p myscript.py但似乎得到了相同的结果。

You aren't returning anything from the script or resetting the value of var1.您不会从脚本返回任何内容或重置 var1 的值。 This answer should help. 这个答案应该会有所帮助。 You would need to write a function in the myscript.py that returns the value you want and then set it to var1 .您需要在myscript.py中编写一个返回所需值的函数,然后将其设置为var1

myscript.py脚本文件

def this_awesome_function():
    return 10

Notebook:笔记本:

from myscript import this_awesome_function

var1 = this_awesome_function()
print(var1) 

Hopefully this should work!希望这应该有效!

A colleague of mine gave the answer below which is the best one I have found so far:我的一位同事给出了以下答案,这是迄今为止我找到的最好的答案:

import subprocess

var1 = 9
subprocess.Popen(["python3", "myscript.py"])
print(var1)

I am not sure that jupyter notebook allows you to do this.我不确定 jupyter notebook 是否允许您执行此操作。 It is most likely using the %run or %prun but can't figure it out.它很可能使用%run%prun但无法弄清楚。

Not with %run presently (see bottom section);目前不使用%run (见底部); however, you don't need to run a bash script from inside your notebook or invoke subprocess .但是,您不需要从笔记本内部运行 bash 脚本或调用subprocess You can just send the command to a separate shell instance and run the script without affecting your local namespace using the following:您可以使用以下命令将命令发送到单独的 shell 实例并运行脚本,而不会影响您的本地命名空间:

!python myscript.py

See 'Shell Commands in IPython' here and sections below that for more information.有关更多信息,请参阅此处的“IPython 中的 Shell 命令”和下面的部分。 Jupyter Python notebooks build on IPython abilities. Jupyter Python 笔记本建立在 IPython 能力之上。


As I advise here , usually you want the more full-featured %run in Jupyter because it handles passing back errors, input prompts, and connects the output from the script better to Jupyter notebooks in general.正如我在这里建议的那样,通常您希望在 Jupyter 中使用功能更全面的%run ,因为它可以处理回传错误、 input提示,并将脚本的输出更好地连接到一般的 Jupyter 笔记本。

So you are thinking well along the line of best practices;所以你沿着最佳实践的路线思考得很好; however, you have overlap with variables in your notebook the main portion of you script that you want to avoid that can easily be done with !python myscript.py .但是,您与笔记本中的变量重叠,您希望避免的脚本的主要部分可以使用!python myscript.py轻松完成。 Normally, there wouldn't actually be much in your main namespace as you modularize your script more with functions.通常,当您使用函数更多地模块化脚本时, main命名空间中实际上不会有太多内容。


I see the text here that makes you think -p option should do as you thought:我在这里看到的文字让您认为-p选项应该按照您的想法执行:

In this mode, the program's variables do NOT propagate back to the IPython interactive namespace (because they remain in the namespace where the profiler executes them).在这种模式下,程序的变量不会传播回 IPython 交互式命名空间(因为它们保留在分析器执行它们的命名空间中)。

It does seem to describe what you are saying, and I am at a loss as to explain why it doesn't.它似乎确实描述了您在说什么,而我无法解释为什么它没有。 I even tried current IPython and the same thing happens.我什至尝试了当前的 IPython 并且发生了同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM