microsoft / pyright

Static Type Checker for Python
Other
13.04k stars 1.39k forks source link

[Need help] About pyrightconfig.json #8347

Closed zbelial closed 1 month ago

zbelial commented 1 month ago

Hi guys, I have some questions about pyrightonfig.json and I haven't found answers in the document.

  1. Will pyright-langserver read pyrightconfig.json automatically?
  2. If the answer to Q1 is yes, where does pyright-langserver find pyrightconfig.json?
  3. If the answer to Q1 is no, what a lsp client can do to let pyright-langserver read it?

Thanks!

I ask these question because I couldn't make it work and don't know why. I created a simple project, in which I used python -m venv to create a virtualenv, then I created a pyrightconfig.json at the root of the project and specified venvPath and venv in it, but it seemed that it didn't work(It couldn't find packages installed in this virtual env).

I used my own lsp client in Emacs. I can provide other info if you need. Thanks.

erictraut commented 1 month ago

Yes, the pyright-langserver will read the pyrightconfig.json or pyproject.toml automatically. It looks in the project root directory passed to the language server. This is assumed to be passed to the language server in the initialization command (in the rootPath field). The language server also supports workspaces, and a different root directory can be passed for each workspace. In that case, a separate instance of the type checker is instantiated for each workspace.

zbelial commented 1 month ago

Hi @erictraut , thanks very for this quick reply.
So a lsp client does not have to support dynamic registretion, or server request (such as workspace/configuration), right? Is there any way to confirm that pyright-langserver has read the pyrightconfig.json file? Or is there any way to debug why does the pyrightconfig.json have no effect?

Following are some info about my testing project. There are too files (bs.py and pyrighgconfig.json) and a virtual env directory (.venv).

  1. The python file, bs.py, has the following content.
    
    import baostock as bs
    import pandas as pd

lg = bs.login()

print('login response error code: ' + lg.code)


2. a pyrightconfig.json file
```json
{
  "include": [
    "venv"
  ],
  "exclude": [
    "**/__pycache__"
  ],

  "pythonVersion": "3.12",
  "pythonPlatform": "Linux",

  "venvPath": "/home/XXXX/tmp/venv",
  "venv": ".venv"
}
  1. and the .venv directory (baostock and pandas are installed in it), the tree structure of it (files and directories in deeper subdirs are not showed):
    .venv
    ├── bin
    ├── include
    │   └── python3.12
    ├── lib
    │   └── python3.12
    │       └── site-packages
    │           ├── baostock
    │           ├── baostock-0.8.9.dist-info
    │           ├── dateutil
    │           ├── numpy
    │           ├── numpy-2.0.0.dist-info
    │           ├── numpy.libs
    │           ├── pandas
    │           ├── pandas-2.2.2.dist-info
    │           ├── pip
    │           ├── pip-24.0.dist-info
    │           ├── __pycache__
    │           ├── python_dateutil-2.9.0.post0.dist-info
    │           ├── pytz
    │           ├── pytz-2024.1.dist-info
    │           ├── six-1.16.0.dist-info
    │           ├── tzdata
    │           └── tzdata-2024.1.dist-info
    └── lib64 -> lib

When I enabled my lsp client (lspce), it told that baostock and pandas could not be resolved. Any advice? Thanks.

erictraut commented 1 month ago

The source code for pyright is open source. Here is the initialization callback for the language server. If you'd like to insert debug statements or connect a debugger to help you diagnose the problem, there are some instructions here. I would also normally suggest adding a "verboseOutput": true to your config file to tell pyright to output more verbose logs, but that presupposes that pyright is able to locate and read the config file.

erictraut commented 1 month ago

Converting this to a discussion since this isn't a bug report.