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.

$PYTHONPATH in notebook is different than in terminal

See original GitHub issue

Environment data

  • VS Code version: 1.65.2
  • Jupyter Extension version (available under the Extensions sidebar): v2022.2.1030672458
  • Python Extension version (available under the Extensions sidebar): v2022.2.1924087327
  • OS (Windows | Mac | Linux distro) and version: Linux
  • Python and/or Anaconda version: Python 3.9.10
  • Type of virtual environment used: conda
  • Jupyter server running: Local

Expected behaviour

$PYTHONPATH should be the same between a notebook and the integrated terminal.

Actual behaviour

After restarting vscode and opening a fresh jupyter notebook, when I run

%%bash
echo $PYTHONPATH

in a notebook cell, the variable value is different than when I run the same command in the integrated terminal

echo $PYTHONPATH

Furthermore, I opened the notebook in JupyterLab and ran the cell and the $PYTHONPATH variable matches the value of the integrated terminal.

Summary: $PYTHONPATH variable matches between terminal and JupyterLab, but not in the vscode notebook.

Steps to reproduce:

Sorry, I’m not sure how to reproduce this.

I did change the value of the PYTHONPATH variable a few days ago. Is it possible that vscode notebook is using a cached value of PYTHONPATH that needs to be reset?

Thanks for any help you can give.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
DonJayamannecommented, Mar 30, 2022

That explains it, when we launch the kernels, the extension does not read bashrc files. In fact we don’t read any shell scripts the user may have configured. To load variables from a shell script we’d need to initialize a shell, and this has a number of problems associated with that (it can be very slow, sometimes even slow down VS Code completely and cause perf issues).

I’m sorry, however at this stage I don’t see a way around this, except for adding the PYTHONPATH path value into your settings.json as follows:

  • Create a .env file in a workspace folder that you open (add the value for PYTHONPATH into that file PYTHONPATH=xyz)
  • The extension will automatically load env variables from this .env file

Optionally, if you would like this to apply to all workspace folders, then please do file an issue in this repo and we’ll look into that. Basically today you need a .env file per workspace, if you’d like to share that across all workspaces we don’t support that, but should be easy to do (as the Python extension supports that).

Closing this issue, as we’re not going to support reading the shell scripts and the work around is to use a .env file.

0reactions
blaylockbkcommented, Mar 30, 2022

Thanks for your help. I’ll make the necessary changes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sys.path different in Jupyter and Python - how to import own ...
Here is what I do on my projects in jupyter notebook, import sys sys.path.append("../") # go to parent dir from customFunctions import *....
Read more >
Sys.path is different than terminal and ipython and ...
Hi,. I am running jupyter notebook within a python 3.9.12 conda environment. I have a project with the following structure
Read more >
Settings Reference for Python - Visual Studio Code
Specifies whether to activate the currently open terminal when the Python extension is activated, using the virtual environment selected. logging.level, error ...
Read more >
Finding your Anaconda Python interpreter path
Open a terminal window. · If you want the location of a Python interpreter for a conda environment other than the root conda...
Read more >
Set up a working environment | DataSpell Documentation
To execute notebooks or Python code in DataSpell, you need to configure ... You can select any other Python environment on your machine...
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