sys.path behavior in vscode insiders is not equivalent to jupyter lab when using a virtual env

215 Views Asked by At

When switching to vscode-insiders notebooks instead of jupyter lab, I noticed that importing my local src package installed using setuptools was not possible anymore, it throws a ModuleNotFoundError.

After comparing the two IDEs, it seems like vscode does not change sys.path as jupyter lab does.

My project architecture:

foo
└── bar
    └── myproject
        ├── notebooks
        │   └── nb.ipynb
        ├── setup.py
        ├── env
        └── src

My installation steps:

cd /foo/bar/myproject
source env/bin/activate
(env) python3 -m pip install -e .
(env) python3 -m ipykernel install --user --name=my_project_env # add the jupyter kernel based on the environment

Then opening nb.ipynb in both jupyterlab and vscode and checking sys.path gives the following:

jupyter lab:

print(sys.path)
-->
...
foo/bar/myproject # allows to import src
...

vscode:

print(sys.path)
-->
...
foo/bar/myproject/notebooks # does not allow to import src
...

I would have expected vscode to add the foo/bar/myproject directory to sys.path as it is the one where src is located, am I getting something wrong?

(In the meantime I found the following workaround, adding the following to my settings.json in vscode:

"jupyter.runStartupCommands":  [
        "import sys",
        "sys.path.insert(2,'${workspaceFolder}')"
    ]

)

0

There are 0 best solutions below