If you're having trouble with the language server, check the below for information which may help. If something isn't covered here, please file an issue with the information given in Filing an issue.
If you're getting a warning about an unresolved import, first ensure that the
package is installed into your environment if it is a library (pip
, pipenv
, etc).
If the warning is about importing your own code (and not a library), continue reading.
The language server treats the workspace root (i.e. folder you have opened) as
the main root of user module imports. This means that if your imports are not relative
to this path, the language server will not be able to find them. This is common
for users who have a src
directory which contains their code, a directory for
an installable package, etc. Note that the src
scenario is automatically detected
by the language server, so no configuration is necessary in that particular case.
These extra roots must be specified to the language server. The easiest way to
do this (with the VS Code Python extension) is to create a workspace configuration
which sets python.analysis.extraPaths
. For example, if a project uses a
sources
directory, then create a file .vscode/settings.json
in the workspace
with the contents:
{
"python.analysis.extraPaths": ["./sources"]
}
This list can be extended to other paths within the workspace (or even with code outside the workspace in more complicated setups). Relative paths will be taken as relative to the workspace root.
Note that if you are coming to Pylance from using the Microsoft Python Language Server, this setting has changed from python.autoComplete.extraPaths
to python.analysis.extraPaths
.
PEP 660 enables build backends (ex. setuptools) to
use import hooks to direct the import machinery
to the package's source files rather than using a .pth
file. Import hooks can provide
an editable installation that is a more accurate representation of your real installation.
However, because resolving module locations using an import hook requires executing Python
code, they are not usable by Pylance and other static analysis tools. Therefore, if your
editable install is configured to use import hooks, Pylance will be unable to find the
corresponding source files.
If you want to use static analysis tools with an editable install, you should configure
the editable install to use .pth
files instead of import hooks. See your build backend's
documentation for details on how to do this. We have provided some basic information for
common build backends below.
Setuptools currently supports two ways to request
"compat mode"
where a .pth
file will be used -- a config setting and an environment variable. Another
option is "strict mode"
which uses symlinks instead.
Hatchling uses .pth
files by
default. It will only use import hooks if you set dev-mode-exact
to true
.
PDM uses .pth
files by default. It will only use import hooks if you set editable-backend
to
"editables"
.
If you are moving from the Microsoft Python Language Server over to Pylance, a good place to start is by reading our migration doc which outlines a couple notable changes between the language servers.
To use Pylance, you will need to be using VS Code version 1.57 or above.
To use Pylance, you must be running an official build of VS Code. If you've verified that you are running an official build and are still running into issues, please file a bug report.
If you are using Pylance in a WSL environment, make sure your workspace is located under a WSL folder (/home/...
) and not shared with Windows (/mnt/...
).
See issues #1443 and vscode-remote-release#5000.
When filing an issue, make sure you do the following:
-
Check existing issues for the same problem (also see the "Known Issues" section above for widespread problems).
-
Enable trace logging by adding
"python.analysis.logLevel": "Trace"
to your settings.json configuration file.- Adding this will cause a large amount of info to be printed to the Python output panel. This should not be left long term, as the performance impact of the logging is significant.
-
Select "View: Toggle Output" from the command palette (Ctrl+Shift+P on Windows/Linux, Command+Shift+P on macOS), then select "Python Language Server" in the dropdown on the right.
-
Copy the entire log starting with "Pylance language server XXX (pyright xxx) starting"
-
State the environment where your code is running; i.e. Python version, the virtual environment type, etc.
- If using a virtual environment, please include the requirements.txt file.
- If working with a conda environment, attach the environment.yml file.
-
A code example (or any other additional information) we can use to reproduce the issue.