Support adding imports automatically on file save like Go (gopls) #6033
Replies: 9 comments 2 replies
-
Auto-import doesn't happen when you save your file, but rather it's a special kind of completion suggestion that not only completes the name you are currently typing, but also adds the relevant import at the top of your file. For example, if I have an empty file and type And after selecting that suggestion for |
Beta Was this translation helpful? Give feedback.
-
Oh... Thank you for the explanation. I'm used to the world of Go where gopls will add the import for you automatically, and it advertises this feature as "auto import". This pylance feature is more of an "automatic import suggestion", or an "import suggestion". Personally I think the terminology is odd and misleading, the docs are a bit misleading as well: "This enables import statements to be automatically added as you type." - this makes it sound as if it'll automatically add it to the file without an extra click needed. It does say "auto import suggestions" before that line, but this later line contradicts that imo. Perhaps it's worth adding extra clarification in the docs, or rewording things a little bit? When I first noticed this I went to the docs to make sure this is how it was supposed to behave (and was not just a suggestion), and that line threw me. That's why I made sure to mention what I was expecting here, hoping you'd catch that if my understanding of the feature was wrong, as you did :-). Suggest: "This enables import statements to be automatically added as you type." -> "This enables import statements to be automatically added to the completions menu as you type." I know it's just a minor thing, but extra clarity is always nice. |
Beta Was this translation helpful? Give feedback.
-
It took me a bit to track down where you were seeing this. I found it at https://code.visualstudio.com/docs/python/editing#_enable-auto-imports which says:
We already mention "suggestions" and "completions" in this text, but I can see that if you have previous experience with other tools/languages using the term "auto-import" to mean something different, it would still be confusing. I think adding a screenshot here might be the easiest way to clarify this. @luabud, what do you think? Btw, I checked the following other places and they all mention completions, so I think they're already good: The setting description:The "DETAILS" pane for the Pylance extension within VS Code:The pylance-release repo
|
Beta Was this translation helpful? Give feedback.
-
Yes I agree, thanks for bringing this up! |
Beta Was this translation helpful? Give feedback.
-
we could bring new feature to pylance - |
Beta Was this translation helpful? Give feedback.
-
How would it resolve any ambiguity -- where the same symbol name is available in multiple packages/modules? @nullbio, how do you use this feature in Go? If the tooling doesn't know where you're importing a particular symbol from yet (before saving), doesn't that mean that you won't get IntelliSense (completion suggestions) for usages related to that symbol? That seems like it would be painful. |
Beta Was this translation helpful? Give feedback.
-
@debonte Yes that's correct, the tooling would need to know where you are importing from. In terms of ambiguity, if the user provides enough information via namespacing at the call site, it can solve it that way. If not, they can resolve it manually. Go naturally promotes conventions that avoid conflicts as much as possible, although they're still possible and it isn't a requirement to do this - we could still have a really great experience in Python. The tooling would show all conflicting options in the case of conflicts, and filter down as more information is added. This might sound like it could be messy on the surface, but in practice it's not a problem - conflicts aren't common unless you're using no namespacing at all (like a whole bunch of * imports), and if you follow a few good conventions it becomes a complete non-issue. The way it works for vscode, is by using the tool called "gopls". Vscode runs the gopls binary in server mode (command: For example, if my folder structure is:
In main.go, if I type: "foo.", it will list all functions in the foo package (all the functions defined in bar.go, in this case) in the intellisense dropdown. Let's say I have a function If I type the whole thing
In python's case, I suppose the equivalent would be if you had:
And you typed:
In addition, without anything imported already, if you typed a., it would dropdown the intellisense with "b", and "b.module", and "b.module.FuncName", without having to add each one as an import at each step, so you can just pick what you want straight away and get the whole thing and have it move your cursor for you, ready to enter fn params. This is also nice because it helps you easily understand a libraries packaging structure and available objects at a glance. It also works for non-namespaced imports as well, but that requires a manual import. For example if I do:
Generally though, in Go, all of your imports are namespaced and it's frowned upon to do dot imports. This does also make the default usecase quite sane. In pylance, you could adopt the following standard convention (with settings overrides), if you wanted to suggest a workflow that minimizes conflicts: All imports should be absolute, and namespaced to the module name. Users should avoid creating duplicate module names. For example:
Then if typing
Python equivalent:
More details of how Gopls and dependency management works if you're curious: I believe Gopls detects the relevant workspace root directory by the presence of a go.mod file at your project root, which defines workspace path. For example, in a project root folder called go.mod
Any external dependencies my project has are installed by running
Go get command will download and install the dependency in my
More info here about how Go dependencies work: https://go.dev/doc/modules/managing-dependencies The way this works in practice when you're coding in Go is an incredibly nice user experience, and it's hard to convey just how practical and intuitive it is through description alone. I really recommend having a play with it to contrast it to pylance's workflow, you'll get a ton of really good ideas on how you can streamline the pylance workflow even further. |
Beta Was this translation helpful? Give feedback.
-
@nullbio, I appreciate the detailed response! To track the idea of automatically adding imports on save like in Go, I'm moving this issue to discussions as an enhancement request. We'll keep an eye on how many up-votes it gets and will investigate this further if there's sufficient interest from other users. The documentation change has merged and will show up on the docs page when it gets published next. Soonish. I don't know their schedule. |
Beta Was this translation helpful? Give feedback.
-
I had a similar behavior of why sometimes i'd get imports automatically added with completions and sometimes not. |
Beta Was this translation helpful? Give feedback.
-
Environment data
VS Code version:
This failure was found in the above, however it is also failing when I test in WSL2 on Ubuntu 24.04, python 3.12.3, using the same extension versions. So it doesn't seem to be tied to the operating system, but rather VSCode and/or Pylance itself.
Code Snippet
Code example and screenshots (doesn't matter what the code is, no matter what happens I never get an auto-import added to my file imports when I save my file. I tried it with just an os line, just a logging line (with the logging package installed using pip, inside my venv, with my venv activated).
The yellow quick fix bulb icon does appear when I hover over the underlined code, and it does suggest the correct import. Clicking it and the "add import" line does work. It's just that it doesn't automatically add the imports.
I fully stripped down my vscode to test this (removed all my user settings except for this in the screenshot)
The only extensions I have enabled are:
Pylance, Python, Python Debugger
I've also tried stripping down the settings further to just this, which also does not work:
Output logs, Python:
Output logs, Python Language Server:
Output logs, Python Debugger:
Output logs, Extension Host:
Beta Was this translation helpful? Give feedback.
All reactions