使用Jupyter Cell Magic时环境变量的作用域 [英] Scoping of environment variables when using Jupyter cell magics

查看:138
本文介绍了使用Jupyter Cell Magic时环境变量的作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解Jupyter笔记本中变量作用域的工作原理.

I would like to understand how variable scoping works in Jupyter notebooks.

当我创建带有两个单元格的 bash 笔记本时,导出的环境变量在单元格边界之间是可见的:

When I create a bash notebook with two cells, environment variables that are exported are visible across cell boundaries:

在[1]中:

export PATH=$PATH:~/samplepath

在[2]中:

echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/myuser/samplepath

但是,如果我创建一个 Python 笔记本并使用单元魔术来达到相同的结果,则变量似乎不再在单元边界上可见:

But if I create a Python notebook and use cell magics to achieve the same result, variables do not seem to be visible across cell boundaries any more:

在[1]中:

%%script bash
export PATH=$PATH:~/samplepath

在[2]中:

%%script bash
echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

对于不同的魔术,此行为保持不变(即,在回声之前使用感叹号而不是脚本魔术会产生相同的结果).

This behaviour stays the same for different magics (i.e., using an exclamation mark in front of echo instead of the script magic gives the same result).

因此,我想了解这种情况下的范围规则,以及如何使export真正导出变量,以便它们在整个笔记本中可见.

So, I'd like to understand what the scoping rules are in this case, and how I can make export truly export variables so that they are visible in the entire notebook.

推荐答案

在Jupyter笔记本(如您的第二个示例)中,笔记本中的shell命令在一个临时子shell中执行,即每个命令您使用!(或在%%bash下)编写的代码在另一个子进程中运行.

In Jupyter notebook (as in your second example), the shell commands in the notebook are executed in a temporary subshell i.e. every command you write using ! (or under %%bash) runs in a different subprocess.

此博客文章是:

Jupyter Notebook外壳程序命令在外壳程序中执行,该外壳程序是运行笔记本程序的外壳程序的子进程.因此,子(单元)外壳中环境变量的变化不会反映在父(笔记本)外壳中.

The Jupyter Notebook shell commands are executed in a shell that is a subprocess of the shell running the notebook. Because of this, changes in environmental variables in the child (cell) shell will not be reflected in the parent (notebook) shell.

在[1]中:

%%bash 
export var="5"
echo $var

5

在[2]中:

%%bash
echo $var

无输出

这就是为什么不能使用!cd /path/之类的命令来导航文件系统的原因.查看另一篇博客文章对此.

This is the very reason why the commands like !cd /path/ can't be used to navigate the file system. Check another blog post on this.

bash笔记本的情况不同,一切都在单个shell中执行.

While the case is different with bash notebook, where everything is executed in a single shell.

解决方案:

如果要将变量导出到环境外壳变量,则可以使用 %env 魔术功能如下:

If you want to export a variable to environment shell variables, then you can use %env magic function as follows:

在[3]中:

%env var "5"

在[4]中:

%env var

在[5]中:

%%bash
echo $var

In [4]和In [5]的输出:

Output of In[4] and In[5]:

'"5"'

注意:但要注意,不要执行%env PATH=$PATH:~/samplepath,因为它只会替换PATH变量,从而引起问题.仅建议将以上解决方案用于非系统变量.不过,您可以编辑.bashrc以普遍更改PATH变量.

Note: But beware, dont't do %env PATH=$PATH:~/samplepath as it'll simply replace the PATH variable thus causing problems. The above solution is recommended for non-system variables only. Nevertheless, you can edit .bashrc to universally change the PATH variable.

这篇关于使用Jupyter Cell Magic时环境变量的作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆