Paste the following configuration, replacing the placeholder text with the path you copied in Step 1:
Solution 1: Tell Poetry to Create Local Virtual Environments (Recommended)
If you already initialized your project, you must delete the old centralized environment and force Poetry to build a new one inside your project directory. Find the path of the current environment: poetry env info Use code with caution.
Before fixing the problem, you must understand it. Pylance needs to know exactly where your installed packages live (the site-packages folder).
Understanding the "why" is the first step to a permanent fix. The root causes fall into a few common categories: pylance missing imports poetry link
: If it's not listed, run poetry env info --path in your terminal to get the exact path, then choose "Enter interpreter path" in VS Code and paste the path to the python executable (e.g., ./.venv/bin/python ). 2. Configure Poetry for Local Virtual Environments
This command will create a poetry.lock file in your project root.
: Find the environment that specifically lists your project name or "Poetry" in the path.
: Press Ctrl+Shift+P (or Cmd+Shift+P on Mac). Find Interpreter : Type "Python: Select Interpreter". Pylance needs to know exactly where your installed
Open the Command Palette and run Python: Restart Language Server . This forces Pylance to clear its cache and rescan your paths.
要解决问题,首先需要理解它的本质。
This happens because Pylance, the language server for Python in VS Code, cannot find the specific virtual environment where Poetry has tucked away your packages. Here is how to link them and clear those errors. 1. Select the Correct Interpreter
The most effective fix is manually pointing VS Code to your Poetry environment. In VS Code
Use Python: Select Interpreter to choose the .venv/bin/python . Restart Pylance if necessary ( Developer: Reload Window ).
This error happens because VS Code and Pylance do not automatically know where Poetry isolates your project's virtual environment. Pylance searches your global Python installation instead of the local environment containing your dependencies.
: If the error persists, run the "Developer: Reload Window" command. Why This Happens with Poetry
Non-invasive, works with existing Poetry setup. Cons: Requires maintaining absolute paths (or using $workspaceFolder but Poetry’s cache is often outside the project).
In VS Code, open the Command Palette ( Cmd+Shift+P or Ctrl+Shift+P ).