skip to Main Content

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).:

enter image description here

enter image description here

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?

2

Answers


  1. Chosen as BEST ANSWER

    After dr. rAI pointed out some useful diagnostics instructions in his answer, I found a clue. While any of instructions in dr. rAI's answer applied, searching around Github and Google with his Pylance settings names gave a hint. Some other people were having the same issue.

    Visual Studio Code's PyLance implementation seems to have some internal limits that may prevent indexing all files. However, this was not the case for me. Instead, PyLance was somehow corrupted.

    Running: PyLance: Clear all persistent indices from the command palette fixed the issue for. After this, PyLance seemed to behave.


  2. I had a similar case a few weeks back and documented the case with the following steps … please try:
    The key for me to resolving this problem was in ensuring VS Code is correctly configured to use the Python interpreter from the Poetry virtual environment.

    Step 1: Ensure Correct Python Interpreter is Selected

    1. Open the Command Palette in VS Code by pressing Ctrl+Shift+P (or Cmd+Shift+P on macOS).
    2. Type Python: Select Interpreter and select it.
    3. Look for the interpreter that corresponds to the Poetry virtual environment for your project. It might be named after your project folder or have a custom name given by Poetry.
    4. Select this interpreter.

    Step 2: Check the Python Extension and PyLance Settings

    Ensure that your VS Code settings are configured to use PyLance and to respect the Poetry virtual environment. You can check or add these settings in your settings.json file (accessible via Ctrl+, and then clicking on the {} icon in the top right corner):

    {
        "python.languageServer": "Pylance",
        "python.analysis.indexing": true,
        "python.analysis.typeCheckingMode": "basic", // Or "off" if you prefer
        "python.poetryPath": "poetry",
        "python.venvPath": "~/.cache/pypoetry/virtualenvs",
        "python.venvFolders": [
            ".venv",
            "venv",
            "env",
            ".env",
            "envs",
            ".pyenv",
            ".direnv"
        ]
    }
    

    Make sure the python.poetryPath is correct and points to where your Poetry CLI is installed. The python.venvPath should be set to the location where Poetry creates its virtual environments if it’s not in the project folder.

    Step 3: Verify Environment Activation

    After selecting the correct interpreter, it’s crucial to restart VS Code or reload the window to ensure that the changes take effect and the selected environment is activated.

    Step 4: Diagnose with the Python Extension Output

    1. Open the Output panel in VS Code (View -> Output).
    2. Select "Python" in the dropdown menu within the Output panel.
    3. Check the logs for any errors or warnings related to environment selection, package resolution, or PyLance.

    Step 5: Use .env File

    Poetry sets environment variables that are necessary for its virtual environment to work properly. You can create a .env file in your project root with the content like:

    PYTHONPATH=<path-to-your-poetry-venv-site-packages>
    

    Replace <path-to-your-poetry-venv-site-packages> with the actual path to the site-packages directory inside your Poetry virtual environment. This approach helps VS Code (and thus PyLance) to recognize where to look for installed packages.

    Additional Tips

    • Ensure your Poetry virtual environment is activated when you open the terminal in VS Code. You can activate it by running poetry shell or source <path-to-venv>/bin/activate (the path might differ depending on your OS).
    • If dependencies are still not recognized, consider closing and reopening VS Code, as sometimes extensions may require a restart to pick up changes in the environment.

    Try this – hope it helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search