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:
- set the env variable and run my notebook (see HELLO_ALICE)
- edit .env file: change "HELLO_ALICE" to "HELLO_BOB"
- set the env variable and run my notebook (see HELLO_BOB)
What do not work:
- open my project in vsCode, open terminal
- in terminal run:
>> set -a; source .env; set +a;
- open notebook, run cell --> I see HELLO_ALICE
- edit .env (change HELLO_ALICE TO HELLO_BOB)
- restart notebook (either click on restart or close tab and reopen it)
- in terminal run:
>> set -a; source .env; set +a;
(same as step 2) - 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:
- VsCode version = 1.63.2
- I tired to use dotenv module and load env variable in my notebook (does not work)
- question: How to set env variable in Jupyter notebook works only if you define your env variables inside notebook
- this behavior happen only on env variables. For instance if instead a .env file I use a env.py file where i define my env constants as python variables, restarting the notebook will refresh the constants.