question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jupyter notebook doesn't load env variables

See original GitHub issue

Does this issue occur when all extensions are disabled?: Can’t check because i only have a remote ssh access to the concerned computer.

  • VS Code Version: Version: 1.62.1 Commit: f4af3cbf5a99787542e2a30fe1fd37cd644cc31f Date: 2021-11-05T10:56:50.217Z Electron: 13.5.2 Chrome: 91.0.4472.164 Node.js: 14.16.0 V8: 9.1.269.39-electron.0 OS: Linux x64 5.4.0-89-generic snap (Ubuntu 18.04.5 LTS)

I’m not sure how to reproduce this issue since it mysteriously stopped working overnight. Anyway, here what happens:

  • I’m running vs-code remotely via SSH. I’m running python inside a venv. I have a variable inside my .bashrc file alike:
export SOME_VARIABLE=/some/path/

so that in a python file, os.getenv(‘SOME_VARIABLE’) works just fine.

  • However inside a jupyter notebook (with the good kernel), this variable is set to None. If it can help, the PYTHONPATH is: /path/to/my/home/.vscode-server/extensions/ms-toolsai.jupyter-2021.10.1101450599/pythonFiles:/path/to/my/home/.vscode-server/extensions/ms-toolsai.jupyter-2021.10.1101450599/pythonFiles/lib/python

  • I tried setting:

    "terminal.integrated.inheritEnv": true,
    "terminal.integrated.defaultProfile.linux": "bash",

in the json settings file but it didn’t change anything.

I guess this is a jupyter bug since it works in a python file.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
rchiodocommented, Feb 4, 2022

Sorry but that’s not supported. Your .bashrc won’t be run before we start a kernel.

You could workaround this by setting this value to set some environment variables:

"jupyter.runStartupCommands": []

Or we could change this issue to an enhancement and add an option to run a kernel from a shell script instead of launching it directly.

2reactions
rchiodocommented, Feb 4, 2022

Even if I export the environment variables through the terminal within VSCode before or while the jupyter notebook is active, I am unable to access them.

Sorry I’m not following what you’re doing here.

VS code itself has to be started from a terminal with the environment variables set. Like so:

rich@CORTANA:~/test$ export FOO=BAR
rich@CORTANA:~/test$ code . <-- This is launching VS code from this folder. It will inherit FOO=BAR

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Notebook cannot load the environment variables automatically
I set my envrionment varibale in ~/.bashrc: export QT_API=pyqt It shows correctly in the terminal, when I ran export.
Read more >
How to set env variable in Jupyter notebook - Stack Overflow
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...
Read more >
How to NOT embedded credential in Jupyter notebook | Medium
How to automatically load environment variables when open Jupyter Notebook · Navigate to the folder %USERPROFILE%\.ipython\profile_default\startup in Windows or ...
Read more >
Read Environment Variables in a Python Notebook
To read environment variables from a Python script or a Jupyter notebook, you would use this code—assuming you have a .env file in...
Read more >
How to Set and List Environmental Variable in Python and ...
In this short guide, you'll see how to set environmental variables in Jupyter Notebook and JupyterLab. Below you can find most popular options:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found