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).:
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
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.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
Ctrl+Shift+P
(orCmd+Shift+P
on macOS).Python: Select Interpreter
and select it.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 viaCtrl+,
and then clicking on the{}
icon in the top right corner):Make sure the
python.poetryPath
is correct and points to where your Poetry CLI is installed. Thepython.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
View -> Output
).Step 5: Use
.env
FilePoetry 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:Replace
<path-to-your-poetry-venv-site-packages>
with the actual path to thesite-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
poetry shell
orsource <path-to-venv>/bin/activate
(the path might differ depending on your OS).Try this – hope it helps!