jupyter-lsp / jupyterlab-lsp

Coding assistance for JupyterLab (code navigation + hover suggestions + linters + autocompletion + rename) using Language Server Protocol
https://jupyterlab-lsp.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.82k stars 148 forks source link

Can Jupyterlab-lsp/pyright work with custom pyrightconfig.json or pyproject.toml files? #754

Open sntgluca opened 2 years ago

sntgluca commented 2 years ago

Elevator Pitch

I'd like that pyright would be able to recognise the custom pyrightconfig.json or pyproject.toml foles in my projects, as described in the pyright documentation.

Motivation

I usually work with a central version of JupyterLab (via JupyterHub), and use different conda environments/kernels to run specific code for my projects. To the best of my understanding, currently pyright recognises only the content from the central environment running Jupyterlab, but doesn't recognise the content from a kernel environment. Being able to setup pyright based on different projects and dedicated environments would greatly improve the user experience, removing side-effect errors and warnings, and allowing more flexibility in the setup of my environments and project structures.

Design Ideas

I don't know enough about the implementation to share hints on this topic. I hope this can be addressed via pyright configuration.

Thank you for considering this feature :)

krassowski commented 2 years ago

Sorry for the late response, but I would have thought that it already works. Have you encountered problem with either of these approaches?

sntgluca commented 2 years ago

Dear @krassowski ,

I have been swamped by other things I could not follow up. I made several attempts, and for me it doesn't work.

I can't exclude a PEBKAC here, but I was able to make something work with VSCode, on the same directory, while JupyterLab doesn't show any difference.

If there's someone who can share a working config, I would appreciate it

sntgluca commented 2 years ago

I wish to share a couple of screenshots to show what I mean:

I setup a simple example as follows

|-- package/
|    |-- lib.py
|-- test/
|   |-- pyrightconfig.json
|   |-- test.py

I started VSCode and JupyterLab from the test directory, setting pyrightconfig.json to look for packages in .., to find package. VSCode is able to resolve the path, while JupyterLab doesn't

Screenshot 2022-04-09 at 12 05 43 Screenshot 2022-04-09 at 12 08 17

Same file, same starting directory. It's nothing more than annoying, but it would be so nice if it could be investigated by some expert.

Thank you, Gianluca

j-carson commented 1 year ago

I am also having problems trying to get pyright to be able to find imports from my current virtual environment by setting venv and venvPath in a pyrightconfig.json file

rk-terence commented 1 year ago

After some experimenting, I have found that if we place the pyproject.toml file inside the directory where we install pyright, the pyright server launched by jupyter-lsp will try to read the configuration file.

For example, if we install pyright in a node_modules directory located at C:/path/to/node_modules/, we can go to C:/path/to/node_modules/pyright/dist/, and create a pyproject.toml file there. From my experience, pyright will be able to recognize it, and no more missing imports!

I don't know why pyright tries to load config files from that directory instead of the jupyter root content directory. My guess is that jupyter-lsp does not pass the correct working directory to pyright, and pyright choose the .js entry file's path as its working directory.

However, although the auto-completion and hinting works now, jump-to-definition functionality seems to be broken when using pyright. I have just filed #1005 describing this in detail.

GuessWhatBBQ commented 6 months ago

I was able to solve the import resolution problem by setting the Language Servers -> pyright -> python.pythonPath config to the path of the python binary created by the virtual environment after finding out that this was also a valid solution from the pyright docs