Skip to content

Latest commit

History

History
132 lines (87 loc) · 8.2 KB

TROUBLESHOOTING.md

File metadata and controls

132 lines (87 loc) · 8.2 KB

Troubleshooting

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.

Known issues

Common questions and issues

Unresolved import warnings

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 asrcdirectory which contains their code, a directory for an installable package, etc. Note that thesrcscenario 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 sourcesdirectory, then create a file.vscode/settings.jsonin 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.extraPathstoPython.analysis.extraPaths.

Editable install modules not found

If you want to use static analysis tools with an editable install, you should configure the editable install to use.pthfiles 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

pip(setuptools) supports two ways to avoid import hooks:

Hatch / Hatchling

Hatchlinguses path-based.pthfiles by default. It will only use import hooks if you setdev-mode-exacttotrue.

PDM

PDMuses path-based.pth files by default. It will only use import hooks if you seteditable-backendto "editables".

Migrating from the Microsoft Python Language Server to Pylance

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.

Minimum VS Code version

To use Pylance, you will need to be using VS Code version 1.57 or above.

Pylance does not start up, or shows an error on startup

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.

Packages do not resolve on install when using WSL

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.

Pylance is crashing

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 commandPreferences: 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 | Aboutmenu.

Increase memory limit for VS code (remote only)

For those usingvscode-serverremotely, you can increase the memory limit by setting theNODE_OPTIONSenvironment variable in your shell configuration.

On Linux or Mac, addexport NODE_OPTIONS= "--max-old-space-size=8192"to either your.xxx_profileor.xxxrcfile. On Windows, addset NODE_OPTIONS=--max-old-space-size=8192to your batch file to update your system environment variable, or open theSystem Propertieswindow and addNODE_OPTIONS=--max-old-space-size=8192.

For more details, visit--max-old-space-size

Exclude unneeded*.pyfiles from analysis

To minimize memory usage by Pylance, exclude unneeded*.pyfiles 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.jsonin the root directory of each workspace instead of usingsettingssection in a*.code-workspacefile.

Filing an issue

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 Loggingcommand.

    • 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. UsePylance: Stop Loggingto disable trace logging when you are done.
  • 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.