5

In my VS Code settings.json file, I can use the following option to define environment variables:

"terminal.integrated.env.osx" : {
    "MY_ENV": "test"
    "MY_ENVTYPE": "qa"
}

Now whenever, I start a new shell in the workspace, the shell loads with the above environment variables, and I can access them typically with os.environ["MY_ENV"] is my python scripts.

But with the same settings.json, if I try to access the environment variables in a Nupyter Notebook I get None. So my question is, is there a way to define environment variables in VS Code's settings.json file, so whenever I start a new notebook, the environment variables are loaded by default.

Currently the workaround I have found is to add the following code snippet in a top code cell.

import os
os.environ["MY_ENV"] = "test"
os.environ["MY_ENVTYPE"] = "qa"

I am hoping there is a better way to do the same thing.

starball
  • 20,030
  • 7
  • 43
  • 238
monte
  • 1,482
  • 1
  • 10
  • 26
  • You can try [this](https://stackoverflow.com/a/68563776/12444149), if you use a virtual environment you can export variable in `activate` script so every new kernel for notebook will have it set as well – Grekkq Apr 21 '22 at 13:45
  • I don't see the command `jupyter.runStartupCommands` in the intellisense, i think that's removed. – monte Apr 21 '22 at 13:59
  • Then you can try my suggestion of using virtual environment, i just tested it and it works. – Grekkq Apr 21 '22 at 14:03
  • okay, just found out that setting is available only in user setting, not in workspace setting of vscode. I am unclear of how to export variable in activate script, I am using conda for virtual env, can you post that as an answer? that would be really helpful. – monte Apr 21 '22 at 14:05
  • Never used conda but according to [docs](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#set-env-vars) you just have to create `env_vars` file – Grekkq Apr 21 '22 at 14:09
  • cool, thanks for sharing the doc, I will look into it. Thanks again for your time – monte Apr 21 '22 at 14:16
  • I am also looking for an answer without using the `activate` script - because when you change a variable, you have to reload the entire VS Code window with `Developer: Reload Window`. None of the proposed answers make it easy to update env vars using the jupyter env. – j7skov Sep 27 '22 at 13:48
  • I think just restarting the kernel should work fine @j7skov – monte Sep 27 '22 at 14:10
  • @monte - It does not work. I wish it would! I've got a similar question out there - https://stackoverflow.com/questions/73858371/is-there-a-way-to-apply-new-environment-variables-in-a-virtual-environment-in-vs/73861509#73861509 – j7skov Sep 27 '22 at 14:20

3 Answers3

3

We could use python-dotenv to solve this problem. Using "pip install python-dotenv" to install the package. To configure the development environment Please add .env file in the root directory of the project:

.
├── . env
└── test. py

Then we can use the following code to load environment:

%load_ext dotenv
%dotenv
MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13
2

I am using the following command in settings.json to setup environment variables which are specific to just jupyter notebook. Feels a bit hackish though.

Using jupyter.runStartupCommands, I am executing bunch of python commands to define environment variables and load jupyter extension. Note each command is separated by a newline character.

"jupyter.runStartupCommands": [
        "import os\nos.environ['MY_ENV']='mltest'\nos.environ['MY_ENVTYPE']='qa'\n%load_ext autoreload\n%autoreload 2"
    ]
monte
  • 1,482
  • 1
  • 10
  • 26
0

The Jupyter extension supports .env files. You can use that (source).

You can also define environment variables in the kernel's kernel.json file- in the env property (see also this answer post).

I assume the %env and %set_env magics would work as well (see also this answer post).

starball
  • 20,030
  • 7
  • 43
  • 238