How to set env variable in Jupyter notebook

180,288

Solution 1

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

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

Solution 2

You can also set the variables in your kernel.json file:

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.

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

  • Read the documentation at https://jupyter-client.readthedocs.io/en/stable/kernels.html#kernel-specs
  • Run jupyter kernelspec list to 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_name in the new kernel.json file.
  • Add a env dictionary defining the environment variables.

Your kernel json could look like this (I did not modify anything from the installed kernel.json except display_name and 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_PATH which effects how compiled modules (e.g. written in C) are loaded. Setting this variable using %set_env did 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

Solution 3

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

Install python-dotenv:

pip install python-dotenv

Load the .env file in a Jupyter notebook:

%load_ext dotenv
%dotenv

Solution 4

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

Solution 5

A gotcha I ran into: The following two commands are equivalent. Note the first cannot use 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 MYPATH=C:/Folder Name/file.txt

and

import os
os.environ['MYPATH'] = "C:/Folder Name/file.txt"
Share:
180,288
Ehab AlBadawy
Author by

Ehab AlBadawy

Updated on July 12, 2022

Comments

  • Ehab AlBadawy
    Ehab AlBadawy almost 2 years

    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?

  • Ehab AlBadawy
    Ehab AlBadawy almost 8 years
    Thanks Kardaj, exporting the variable in ~/.profile solved it, seems that it's not reading from bashrc which is kinda weird.
  • Sida Zhou
    Sida Zhou over 6 years
    michael's answer with %env MY_VAR=MY_VALUE should be the correct answer to this question
  • michael
    michael almost 6 years
    Have you simply tried quotes? Note that changing the os.environ isn't the same -- it can only change that (in memory, current python process) dict, and doesn't literally set an OS env var (eg for subsequent !shell commands).
  • evan_b
    evan_b almost 6 years
    I deleted my earlier comments as they weren't quite accurate - but note that the %env and %set_env magic commands use os.environ[var] = val on the backend: github.com/ipython/ipython/blob/master/IPython/core/magics/…
  • Him
    Him over 5 years
    @SidaZhou why is michael's answer better?
  • Khurram Majeed
    Khurram Majeed about 5 years
    Can you please advise me how do I add C:\Program Files (x86)\Graphviz2.38\bin\dot.exe to existing system path using your suggested technique? Will it work if I am not using admin account? I am using Windows 10.
  • James Wierzba
    James Wierzba about 5 years
    @michael Is there any way to persist the environment across all notebooks? Setting the environment this way seems to only persist the environment for the current notebook.
  • morganics
    morganics about 5 years
    @SidaZhou depends on use case - if you want creds to be available in env - and don't want creds to be in your notebook (e.g. on source control) then this isn't ideal.
  • Stefan Dragnev
    Stefan Dragnev almost 5 years
    Exactly what I needed. Homebrew's Python overwrites sys.executable unless PYTHONEXECUTABLE is set beforehand, which you have to set before python runs.
  • shadowtalker
    shadowtalker over 3 years
    In my opinion this is the only correct answer, because it uses only Jupyter itself, rather than depending on the functionality being available in any specific kernel.
  • Priyansh Agrawal
    Priyansh Agrawal about 3 years
    @michael, how does one unset an env var?
  • Jonas Palačionis
    Jonas Palačionis over 2 years
    And how do I reach those variable names inside jupyter?
  • Gustavo Seabra
    Gustavo Seabra about 2 years
    This works for me, but remember to add the correct value you want for the environment variable. The way it is written (at least for me in Jupyter Lab) just erases the variable alltogether. For example, to be able to run Tensorflow on a Jupyter Notebook, I used: "env": {"LD_LIBRARY_PATH":"/opt/miniconda3/envs/tensorflow/lib:${LD‌​_LIBRARY_PATH}"}.
  • Royi
    Royi about 2 years
    Could it be it doesn't affect Jupyter on VS Code on Windows?