Search code examples
python-3.xvisual-studio-codejupyter-notebookcondapylance

VSCode Jupiter notebook cannot resolve import for package installed in editable (-e) mode


I am using a Jupyter notebook running in VSCode to test some ideas for a work project and I am using a package that is in a private GitHub repo. I encountered certain conflicts with versions of jax and numpy and after some fiddling trying to fix the issues, I simply re-installed said package to make sure I was using the correct versions of all required packages. Everything seemed fine until I re-opened the notebook and found a bunch of Import "<package name>" could not be resolved Pylance.

The imports still work and the code runs, however, PyLance appears not to be "seeing" said package.

I am using a conda Python 3.8 environment and to re-install the package I ran

pip install -e . --force-reinstall

I have also tried un-installing and re-installing the package, as well as creating a brand new conda env, and then installing the package there. Neither of these options worked however and I was stuck with the same issue.

Upon inspection of what is in sys.path I saw a strange entry

'__editable__.<package_name>-<package_version>.finder.__path_hook__'

I know that a colleague (who does not have this issue) does not see anything similar and instead has the correct local path to the package.

I should also note that I have the Python extension enabled, which includes the Jupyter and Pylance extensions as well. I have tried re-installing those extensions as well and that did not solve the issue either.

Edit.

I will add that I only get this PyLance error in notebooks. If I open a regular Python file then all PyLance features appear to be working as expected.


Solution

  • Try adding the following to your VSCode settings.json file

        "python.analysis.extraPaths": [
            "<path-to-project-root-of-editable-installed-package>",
        ],