Pylance Missing Imports Poetry Link [ FHD 2K ]
如果仅 Pylance 报红而代码实际能运行,说明解释器配置正确;如果代码也无法运行,则需要优先检查 Poetry 环境本身。
Run .This forces Pylance to completely re-analyze your workspace. 2. Verify the Poetry Shell
Run this command in your terminal: poetry config virtualenvs.in-project true pylance missing imports poetry link
Poetry, by default, creates its virtual environments in a centralized cache directory (e.g., ~/Library/Caches/pypoetry/virtualenvs/ on macOS, %APPDATA%\pypoetry\virtualenvs\ on Windows). This is different from the traditional venv folder inside the project root.
VS Code and Pylance look for packages in the globally active Python environment or a local .venv folder within your workspace root. When Poetry saves packages in its global cache, Pylance loses the link between your project files and its external source code. To fix this, you must explicitly link Pylance to Poetry's virtual environment. This is different from the traditional venv folder
Run this command in your terminal to force Poetry to build a .venv folder directly inside your current project root: poetry config virtualenvs.in-project true Use code with caution. Step 2: Rebuild the environment
Use the for new projects. For existing projects, rely on .vscode/settings.json to explicitly declare the interpreter path. By taking control of how Pylance discovers your Poetry environment, you turn a daily annoyance into a seamless, productive workflow. To fix this, you must explicitly link Pylance
将输出的路径(如 ['/path/to/poetry/venv/lib/python3.12/site-packages'] )中的第一个值添加到 extraPaths :