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 inFiling 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 importingyour owncode (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 asrc
directory which contains their code, a directory for
an installable package, etc. Note that thesrc
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 setsPython.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 fromPython.autoComplete.extraPaths
toPython.analysis.extraPaths
.
If you want to use static analysis tools with an editable install, you should configure the editable install to use.pth
files that contain file paths rather than executable lines (prefixed withimport
) that install import hooks. See your package manager’s documentation for details on how to do this. We have provided some basic information for common package managers below.
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.
pip
(setuptools
) supports two ways to avoid import hooks:
Hatchlinguses path-based.pth
files by
default. It will only use import hooks if you setdev-mode-exact
totrue
.
PDMuses path-based.pth
files by default. It will only use import hooks if you seteditable-backend
to
"editables"
.
If you are moving from the Microsoft Python Language Server over to Pylance, a good place to start is by readingour migration docwhich 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#1443andvscode-remote-release#5000.
Although we attempt to prevent Pylance from crashing, sometimes certain configurations can cause problems for Pylance. One particular problem is the amount of memory that Pylance is allowed to allocate when running inside of VS Code. VS Code ships withpointer compressionenabled. This makes VS Code run faster, but limits the amount of memory that Pylance can use. With some configurations, we may need more than 4GB of memory in order to analyze your project.
If you think you're hitting an out-of-memory situation, you can alleviate this problem in a number of ways:
Provide your ownNode.jsexecutable to run Pylance with.
Pylance (by default) runs using VS Code's Node.js executable (which has the 4GB limit).
To specify your own Node.js executable, set this setting in your User settings.json and restart VS Code:
"Python.analysis.nodeExecutable":"<path to node.js exe>"
The location of your User settings.json depends upon how you're connecting:
- Local - Stored in alocalfile. This can be found with the command
Preferences: Open User Settings.json
. - Remote - Stored on the remote machine. Example
/home/user/.vscode-server/data/Machine/settings.json
You should make sure to use a version of node that is greater than or equal to what VS Code is using. You can determine the version of VS Code's node in theHelp | About
menu.
For those usingvscode-server
remotely, you can increase the memory limit by setting theNODE_OPTIONS
environment variable in your shell configuration.
On Linux or Mac, addexport NODE_OPTIONS= "--max-old-space-size=8192"
to either your.xxx_profile
or.xxxrc
file. On Windows, addset NODE_OPTIONS=--max-old-space-size=8192
to your batch file to update your system environment variable, or open theSystem Properties
window and addNODE_OPTIONS=--max-old-space-size=8192
.
For more details, visit--max-old-space-size
To minimize memory usage by Pylance, exclude unneeded*.py
files usingPython.analysis.exclude
.For instance, you can add"Python.analysis.exclude": [ "**/testFiles/*.py" ]
to your.vscode/settings.json
.
For environments with multiple root workspaces, place the.vscode/settings.json
in the root directory of each workspace instead of usingsettings
section in a*.code-workspace
file.
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 or by using thePylance: Start Logging
command.- 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.
Use
Pylance: Stop Logging
to disable trace logging when you are done.
- 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.
Use
-
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.