[Python] PyLance in Visual Studio Code does not recognise Poetry virtual env dependencies

Discussão em 'Python' iniciado por Stack, Janeiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am using Poetry to manage a Python project. I create a virtual environment for Poetry using a normal poetry install and pyproject.toml workflow. Visual Studio Code and its PyLance does not pick up project dependencies in Jupyter Notebook.

    • Python stdlib modules are recognised
    • The modules of my application are recognised
    • The modules in the dependencies and libraries my application uses are not recognised

    Instead, you get an error

    Import "xxx" could not be resolved Pylance (reportMissingImports)


    An example screenshot with some random imports that show what is recognised and what is not (tradeexecutor package is Poetry project, then some random Python packages dependency are not recognised).:

    [​IMG]

    [​IMG]

    The notebook still runs fine within Visual Studio Code, so the problem is specific to PyLance, the virtual environment is definitely correctly set up.

    Some Python Language Server output (if relevant):

    2024-03-01 10:15:40.628 [info] [Info - 10:15:40] (28928) Starting service instance "trade-executor"
    2024-03-01 10:15:40.656 [info] [Info - 10:15:40] (28928) Setting pythonPath for service "trade-executor": "/Users/moo/code/ts/trade-executor"
    2024-03-01 10:15:40.657 [info] [Info - 10:15:40] (28928) Setting environmentName for service "trade-executor": "3.10.13 (trade-executor-8Oz1GdY1-py3.10 venv)"
    2024-03-01 10:15:40.657 [info] [Info - 10:15:40] (28928) Loading pyproject.toml file at /Users/moo/code/ts/trade-executor/pyproject.toml
    2024-03-01 10:15:40.657 [info] [Info - 10:15:40] (28928) Pyproject file "/Users/moo/code/ts/trade-executor/pyproject.toml" has no "[tool.pyright]" section.
    2024-03-01 10:15:41.064 [info] [Info - 10:15:41] (28928) Found 763 source files
    2024-03-01 10:15:41.158 [info] [Info - 10:15:41] (28928) Background analysis(4) root directory: file:///Users/moo/.vscode/extensions/ms-python.vscode-pylance-2024.2.2/dist
    2024-03-01 10:15:41.158 [info] [Info - 10:15:41] (28928) Background analysis(4) started
    2024-03-01 10:15:41.411 [info] [Info - 10:15:41] (28928) Indexer background runner(5) root directory: file:///Users/moo/.vscode/extensions/ms-python.vscode-pylance-2024.2.2/dist (index)
    2024-03-01 10:15:41.411 [info] [Info - 10:15:41] (28928) Indexing(5) started
    2024-03-01 10:15:41.662 [info] [Info - 10:15:41] (28928) scanned(5) 1 files over 1 exec env
    2024-03-01 10:15:42.326 [info] [Info - 10:15:42] (28928) indexed(5) 1 files over 1 exec


    Also looks like PyLance correctly finds the virtual environment in the earlier Python Language Server output:

    2024-03-03 19:36:56.784 [info] [Info - 19:36:56] (41658) Pylance language server 2024.2.2 (pyright version 1.1.348, commit cfb1de0c) starting
    2024-03-03 19:36:56.789 [info] [Info - 19:36:56] (41658) Server root directory: file:///Users/moo/.vscode/extensions/ms-python.vscode-pylance-2024.2.2/dist
    2024-03-03 19:36:56.789 [info] [Info - 19:36:56] (41658) Starting service instance "trade-executor"
    2024-03-03 19:36:57.091 [info] [Info - 19:36:57] (41658) Setting pythonPath for service "trade-executor": "/Users/moo/Library/Caches/pypoetry/virtualenvs/trade-executor-8Oz1GdY1-py3.10/bin/python"
    2024-03-03 19:36:57.093 [info] [Info - 19:36:57] (41658) Setting environmentName for service "trade-executor": "3.10.13 (trade-executor-8Oz1GdY1-py3.10 venv)"
    2024-03-03 19:36:57.096 [info] [Info - 19:36:57] (41658) Loading pyproject.toml file at /Users/moo/code/ts/trade-executor/pyproject.toml


    How to diagnose the issue further and then fix the issue?

    Continue reading...

Compartilhe esta Página