skip to Main Content

When I type out something like np. I think this triggers Visual Studio Code + Pylance’s (not sure) auto-import completion by suggesting that import numpy as np might be relevant.

I would like to create similar custom auto-import/complete associations. For example: between pl and polars, so that if I type something like pl. then import polars as pl is given as an auto-import suggestion.

How can I do this? Is this specific to the Pylance extension I am using, or something about Visual Studio Code?

Please note that auto-import/import-completion is very different from custom code snippets, as covered in How to add custom code snippets in VSCode? The reason for this being:

  • VS Code adds a new import statement (and therefore has to figure out if it is possible to resolve that import) at the top of the file. It does not add code where the cursor is, which is what a snippet would do.
  • This functionality relies on a language server of some sort (hence my suspicion it is Pylance that is providing this functionality) both to resolve the import, and to insert the import statement at the appropriate location in the file.

2

Answers


  1. These abbreviations seem to be hard-coded in Pylance. As of the latest version (v2024.8.1), the defined abbreviations are:

    • np for numpy
    • pd for pandas
    • tf for tensorflow
    • plt for pyplot/matplotlib
    • mpl for matplotlib
    • m for math
    • spio for io/scipy
    • sp for scipy
    • pn for panel
    • hv for holoviews

    I don’t think you can add new ones from user-side. You can, however, make a request by creating an issue at @microsoft/pylance-release.

    Login or Signup to reply.
  2. This is tracked by an open enhancement request: Allow auto-import abbreviations to be configured #2589. I suggest that you give that discussion an upvote to show support for it. You can also subscribe to it to get notified about discussion and progress. Please avoid making noisy comments there like ones that just consist of "+1" / "bump".

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search