Vince M
Vince M

Reputation: 1186

update env variable on notebook in VsCode

I’m working on a python project with a notebook and .env file on VsCode. I have problem when trying to refresh environment variables in a notebook (I found a way but it's super tricky).

My project:

.env file with: MY_VAR="HELLO_ALICE"

test.ipynb file with one cell:

from os import environ
print('MY_VAR = ', environ.get('MY_VAR'))

What I want:

  1. set the env variable and run my notebook (see HELLO_ALICE)
  2. edit .env file: change "HELLO_ALICE" to "HELLO_BOB"
  3. set the env variable and run my notebook (see HELLO_BOB)

What do not work:

  1. open my project in vsCode, open terminal
  2. in terminal run: >> set -a; source .env; set +a;
  3. open notebook, run cell --> I see HELLO_ALICE
  4. edit .env (change HELLO_ALICE TO HELLO_BOB)
  5. restart notebook (either click on restart or close tab and reopen it)
  6. in terminal run: >> set -a; source .env; set +a; (same as step 2)
  7. open notebook, run cell --> I see HELLO_ALICE

So I see twice HELLO_ALICE instead of HELLO_ALICE then HELLO_BOB...

But if it was on .py file instead of notebook, it would have worked (I would see HELLO_ALICE first then HELLO_BOB)

To make it work:

Replace step 5. by: Close VsCode and reopen it

Why it is a problem:

It is super tricky. I'm sure that in 3 month I will have forgotten this problem with the quick fix and I will end up loosing again half a day to figure out what is the problem & solution.

So my question is:

Does anyone know why it works like this and how to avoid closing and reopening VsCode to refresh env variable stored in a .env file on a notebook ?

(Closing and reopening VsCode should not change behavior of code)

Notes:

Upvotes: 9

Views: 21682

Answers (3)

DrWho
DrWho

Reputation: 74

I'm having a similar issue getting oneAPI's MKL vars loaded on my vscode-jupyter notebook extension.

I found a work around by simply loading the environment vars before opening vscode:

(base) joe@cool$ source /opt/intel/oneapi/setvars.sh 
 
:: initializing oneAPI environment ...
   bash: BASH_VERSION = 5.2.21(1)-release
   args: Using "$@" for setvars.sh arguments: 
:: compiler -- latest
:: mkl -- latest
:: tbb -- latest
:: umf -- latest
:: oneAPI environment initialized ::
 
(base) joe@cool$ code&

But, I hope there is a better way to do this without closing and re-opening vscode every time I need to load a python library built with oneAPI's MKL calls.

Upvotes: 0

Vlad
Vlad

Reputation: 136

The terminal you open in VSC is not the same terminal ipython kernel is running. The kernel is already running in an environment that is not affected by you changing variables in another terminal. You need to set the variables in the correct environment. You can do that with dotenv, but remember to use override=True.

This seems to work:

$ pip3 install python-dotenv
import dotenv
from os import environ
env_file = '../.env'

f = open(env_file,'w')
f.write('MY_VAR="HELLO_ALICE"')
f.close()
dotenv.load_dotenv(env_file, override=True)
print('MY_VAR = ', environ.get('MY_VAR'))

f = open(env_file,'w')
f.write('MY_VAR="HELLO_BOB"')
f.close()
dotenv.load_dotenv(env_file, override=True)
print('MY_VAR = ', environ.get('MY_VAR'))
MY_VAR =  HELLO_ALICE
MY_VAR =  HELLO_BOB

Upvotes: 11

I am having the same problem using the VS Code version you mentioned.

I have found that a quick DIY solution (definitely not the best one) is to use python-dotenv to reload the environment variables yourself on the first cell of the notebook (You also have to restart the notebook after changing the file).

Example:

import dotenv
import os

# Reload the variables in your '.env' file (override the existing variables)
dotenv.load_dotenv(".env", override=True)

# 'MY_VAR' is refreshed now
print('MY_VAR = ', os.environ.get('MY_VAR')) # MY_VAR = HELLO_BOB

Anyhow, I think it might be a bug on the vscode-jupyter extension. I think it would make sense that the environment variables in the .env file are overriden automatically every time you restart the notebook.

Upvotes: 4

Related Questions