Python 如何在 Jupyter Notebook 中设置 env 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37890898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:03:54  来源:igfitidea点击:

How to set env variable in Jupyter notebook

pythonbashenvironment-variablesjupyter-notebook

提问by Ehab AlBadawy

I've a problem that Jupyter can't see env variable in bashrc file, is there a way to load these variables in jupyter or add custome variable to it?

我有一个问题,即 Jupyter 在 bashrc 文件中看不到 env 变量,有没有办法在 jupyter 中加载这些变量或向其中添加自定义变量?

回答by michael

To set an env variable in a jupyter notebook, just use a %magic commands, either %envor %set_env, e.g., %env MY_VAR=MY_VALUEor %env MY_VAR MY_VALUE. (Use %envby itself to print out current environmental variables.)

要设置环境变量在jupyter笔记本电脑,只需用%神奇的命令,无论是%env%set_env,例如,%env MY_VAR=MY_VALUE%env MY_VAR MY_VALUE。(单独使用%env以打印出当前的环境变量。)

See: http://ipython.readthedocs.io/en/stable/interactive/magics.html

请参阅:http: //ipython.readthedocs.io/en/stable/interactive/magics.html

回答by Bernhard

You can also set the variables in your kernel.jsonfile:

您还可以在kernel.json文件中设置变量:

My solution is useful if you need the same environment variables every time you start a jupyter kernel, especially if you have multiple sets of environment variables for different tasks.

如果每次启动 jupyter 内核时都需要相同的环境变量,我的解决方案很有用,尤其是当您为不同的任务设置多组环境变量时。

To create a new ipython kernel with your environment variables, do the following:

要使用环境变量创建新的 ipython 内核,请执行以下操作:

  • Read the documentation at https://jupyter-client.readthedocs.io/en/stable/kernels.html#kernel-specs
  • Run jupyter kernelspec listto see a list with installed kernels and where the files are stored.
  • Copy the directory that contains the kernel.json (e.g. named python2) to a new directory (e.g. python2_myENV).
  • Change the display_namein the new kernel.jsonfile.
  • Add a envdictionary defining the environment variables.
  • https://jupyter-client.readthedocs.io/en/stable/kernels.html#kernel-specs阅读文档
  • 运行jupyter kernelspec list以查看包含已安装内核和文件存储位置的列表。
  • 将包含 kernel.json 的目录(例如 named python2)复制到新目录(例如python2_myENV)。
  • 更改display_namekernel.json文件中的 。
  • 添加env定义环境变量的字典。

Your kernel json could look like this (I did not modify anything from the installed kernel.json except display_nameand env):

您的内核 json 可能如下所示(除了display_name和 ,我没有修改已安装的 kernel.json 中的任何内容env):

{
 "display_name": "Python 2 with environment",
 "language": "python",
 "argv": [
  "/usr/bin/python2",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "env": {"LD_LIBRARY_PATH":""}
}

Use cases and advantages of this approach

这种方法的用例和优点

  • In my use-case, I wanted to set the variable LD_LIBRARY_PATHwhich effects how compiled modules (e.g. written in C) are loaded. Setting this variable using %set_envdid not work.
  • I can have multiple python kernels with different environments.
  • To change the environment, I only have to switch/ restart the kernel, but I do not have to restart the jupyter instance (useful, if I do not want to loose the variables in another notebook). See -however - https://github.com/jupyter/notebook/issues/2647
  • 在我的用例中,我想设置LD_LIBRARY_PATH影响编译模块(例如用 C 编写)加载方式的变量。使用设置此变量%set_env不起作用。
  • 我可以有多个不同环境的 python 内核。
  • 要更改环境,我只需要切换/重新启动内核,但不必重新启动 jupyter 实例(如果我不想丢失另一个笔记本中的变量,则很有用)。见 - 然而 - https://github.com/jupyter/notebook/issues/2647

回答by kardaj

You can setup environment variables in your code as follows:

您可以在代码中设置环境变量,如下所示:

import sys,os,os.path
sys.path.append(os.path.expanduser('~/code/eol_hsrl_python'))
os.environ['HSRL_INSTRUMENT']='gvhsrl'
os.environ['HSRL_CONFIG']=os.path.expanduser('~/hsrl_config')

This if of course a temporary fix, to get a permanent one, you probably need to export the variables into your ~.profile, more information can be found here

这当然是一个临时修复,要获得永久修复,您可能需要将变量导出到您的~.profile,更多信息可以在这里找到

回答by aparkerlue

If you're using Python, you can define your environment variables in a .envfile and load them from within a Jupyter notebook using python-dotenv.

如果您使用 Python,则可以在.env文件中定义环境变量,并使用python-dotenv从 Jupyter notebook 中加载它们。

Install python-dotenv:

安装 python-dotenv:

pip install python-dotenv

Load the .envfile in a Jupyter notebook:

.env在 Jupyter 笔记本中加载文件:

%load_ext dotenv
%dotenv

回答by Baschdl

If you need the variable set before you're starting the notebook, the only solution which worked for me was env VARIABLE=$VARIABLE jupyter notebookwith export VARIABLE=valuein .bashrc.

如果在启动笔记本之前需要设置变量,唯一对我有用的解决方案是env VARIABLE=$VARIABLE jupyter notebook使用export VARIABLE=valuein .bashrc

In my case tensorflow needs the exported variable for successful importing it in a notebook.

在我的例子中,tensorflow 需要导出的变量才能成功地将它导入到笔记本中。

回答by evan_b

A gotcha I ran into: The following two commands are equivalent. Note the first cannotuse quotes. Somewhat counterintuitively, quoting the string when using %env VAR ...will result in the quotes being included as part of the variable's value, which is probably not what you want.

我遇到的一个问题:以下两个命令是等效的。注意第一个不能使用引号。有点违反直觉,在使用时引用字符串%env VAR ...将导致引号作为变量值的一部分包含在内,这可能不是您想要的。

%env MYPATH=C:/Folder Name/file.txt

and

import os
os.environ['MYPATH'] = "C:/Folder Name/file.txt"

回答by Andy D

If you are using systemd I just found out that you seem to have to add them to the systemd unit file. This on Ubuntu 16. Putting them into the .profile and .bashrc (even the /etc/profile) resulted in the ENV Vars not being available in the juypter notebooks.

如果您使用的是 systemd,我刚刚发现您似乎必须将它们添加到 systemd 单元文件中。这是在 Ubuntu 16 上。将它们放入 .profile 和 .bashrc(甚至是 /etc/profile)会导致 juypter 笔记本中无法使用 ENV 变量。

I had to edit:

我不得不编辑:

/lib/systemd/system/jupyer-notebook.service

and put in the variable i wanted to read in the unit file like:

并放入我想在单元文件中读取的变量,例如:

Environment=MYOWN_VAR=theVar

and only then could I read it from within juypter notebook.

只有这样我才能从 juypter notebook 中阅读它。

回答by wingr

A related (short-term) solution is to store your environment variables in a single file, with a predictable format, that can be sourced when starting a terminal and/or read into the notebook. For example, I have a file, .env, that has my environment variable definitions in the format VARIABLE_NAME=VARIABLE_VALUE(no blank lines or extra spaces). You can source this file in the .bashrcor .bash_profilefiles when beginning a new terminal session and you can read this into a notebook with something like,

一个相关的(短期)解决方案是将您的环境变量以可预测的格式存储在单个文件中,该文件可以在启动终端和/或读入笔记本时获取。例如,我有一个文件 ,.env其中包含格式中的环境变量定义VARIABLE_NAME=VARIABLE_VALUE(没有空行或多余的空格)。您可以在开始新的终端会话时在.bashrc.bash_profile文件中获取此文件,并且可以将其读入笔记本中,例如,

import os
env_vars = !cat ../script/.env
for var in env_vars:
    key, value = var.split('=')
    os.environ[key] = value

I used a relative path to show that this .envfile can live anywhere and be referenced relative to the directory containing the notebook file. This also has the advantage of not displaying the variable values within your code anywhere.

我使用了一个相对路径来表明这个.env文件可以存在于任何地方,并且可以相对于包含笔记本文件的目录进行引用。这还有一个优点,即不在代码中的任何地方显示变量值。