Skip to content

Opening Large Workspaces in VS Code

Heejae Chang edited this page Sep 16, 2024 · 2 revisions

Pylance enhances productivity by offering an extensive set of IntelliSense features for Python files in Visual Studio Code. To enable workspace-wide capabilities like symbol or module renaming, as well as code navigation features such as "Go to Definition" and "Find All References", Pylance scans all files within a given workspace once it's opened in VS Code. However, when dealing with very large folders, Pylance may spend significant time enumerating files in subdirectories to provide its IntelliSense features.

Depending on your use case, there are a few options to improve your experience with Pylance:

Open a sub-folder

If you open a large parent folder but find yourself mostly working on a specific part of your project under a subfolder, consider making that subfolder your workspace by opening it in VS Code, instead of the parent folder. Opening a subfolder can simplify Pylance's task by reducing its scope to only relevant files.

Disable specific Pylance features

Disabling certain Pylance features can help manage performance in large workspaces, especially when full functionality is not required for all files. You can explore the Pylance Configuration Tips page to understand how you can adjust settings to minimize resource consumption.

Manually configure your workspace

You can also customize the scope in which Pylance will work on through settings such as python.analysis.exclude and python.analysis.include, which define what should be included or excluded from what Pylance considers as a workspace, as well as python.analysis.extraPaths to define additional import roots.

Here's a detailed explanation of each setting and how to use them effectively.

1- exclude setting

The exclude setting specifies paths to directories or files that Pylance shouldn't consider as part of your workspace. This is useful for omitting irrelevant or temporary files that don't require IntelliSense support, such as build artifacts or dependency directories.

Key Points:

  • By default, Pylance excludes certain directories like **/node_modules, **/__pycache__, .git, and virtual environment directories. However, if you customize this setting, Pylance's automatic exclusion of virtual environments will be overridden, and you’ll need to manually specify which virtual environments to exclude.
  • Paths specified under exclude take precedence over the ones in the include setting, meaning you can fine-tune which parts of your included directories should be ignored.
  • IntelliSense will still be available for excluded files if they're explicitly open in the editor
  • Paths may contain wildcard characters:
    • **: Matches any directory or multiple levels of directories.
    • *: Matches any sequence of zero or more characters.
    • ?: Matches a single character.

Example:

{
 "python.analysis.exclude": ["**/node_modules", "**/__pycache__", "**/build", ".git", "myenv/**"]
}

2- include setting

The include setting specifies paths to directories or files that Pylance should recognize as part of your workspace. This is useful for incorporating directories that may not be directly within the currently open workspace in the editor.

Key Points:

  • By default, Pylance includes everything in the editor's workspace root directory, but note that if you customize this setting, Pylance's automatic inclusion of the entire workspace will be overridden, and you’ll need to manually specify which the workspace folder if you want it to still be included.
  • If you want Pylance to include additional files or directories to enable features such as completions or auto import suggestions, but you don't wish these files to be edited when invoking e.g. rename symbol, consider setting the extraPaths setting instead.
  • Paths can include wildcard characters:
    • **: Matches any directory or multiple levels of directories.
    • *: Matches any sequence of zero or more characters.
    • ?: Matches a single character.

Example:

{
   "python.analysis.include": ["src/**/*", "scripts/**/*"]
}

3- extraPaths setting

By default, Pylance auto detects third party packages through the PYTHONPATH environment variable, as well as what is installed on your selected environment in VS Code -- whether it's a Python interpreter or a Jupyter notebook kernel. However, you can customize the extraPaths to define additional import roots, which can be particularly useful to include third-party packages when they're located in a non-standard location.

Key Points:

  • By default, extraPaths will include common search paths such as src as long as the python.analysis.autoSearchPaths setting is enabled and there is NO __init__.py under the the src folder.
  • Paths can include wildcard characters:
    • **: Matches any directory or multiple levels of directories.
    • *: Matches any sequence of zero or more characters.
    • ?: Matches a single character.

Example:

{
 "python.analysis.extraPaths": ["src/**/*", "common"]
}

Set up mono repos for use in VS Code

If you are working with a mono repo, consider setting it up in VS Code by following this set-up guide: https://github.com/microsoft/vscode-python/wiki/Mono-Repo-Set%E2%80%90up-Guide

Use Light Language Server Mode

If you are working with a large number of Python script files or prefer to use Visual Studio Code as a lightweight text editor with some basic Python IntelliSense features like code completion and hover information, you can configure Pylance to operate in "light" language server mode. This mode turns off resource-intensive features, effectively turning it into a more performance-friendly tool for large projects, with some sacrifice of functionality.

For more details, refer to the Pylance Settings and Customization documentation.