pythonvisual-studio-codejupyter-notebookvscode-remote

Jupyter notebook running in Visual Studio Code cannot import local module, but the same jupyter server can


I started having this issue today, it was working fine last time I used it so I guess something was broken with one of the updates (not surprising).

I'm using VsCode in remote connected to a machine and running a jupyter notebook. I cannot import a module located in a subfolder of my home.

enter image description here

However, if I try to connect to the SAME EXACT server Vscode spawned to run its kernel (I tunnelled 8888 to my localhost and opened in the browser) everything is working.

enter image description here

I checked everything printed in the two notebooks: PATH, PYTHON_PATH, which python. Everything seems to be exactly the same. Needless to say I have many scripts running in the exact same folder where the notebook is located and they're doing just fine (as they've been doing for the past years). Whenever I open a normal python file the language server is working just fine and it finds all the imports.

I'm at a loss because I'm not understanding what is Vscode doing with the PATH or other parameters to mess up the import of local modules. If I try to import something else (like import pandas as pd) it works as expected. There must be something wrong when starting the kernel because the server seems to be spawned with the correct environment.


Solution

  • The problem was related to vscode not setting the right notebook file root. As workaround I had to specify

       "jupyter.jupyterCommandLineArguments": [
            "--notebook-dir='/dir/where/my/noteboook/imports/are/'"
        ],
    

    manually.

    All details are here https://github.com/microsoft/vscode-jupyter/issues/15777 and the issue is https://github.com/microsoft/vscode-jupyter/issues/8771