Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-import doesn't work #28

Closed
imankulov opened this issue Jul 2, 2020 · 41 comments
Closed

Auto-import doesn't work #28

imankulov opened this issue Jul 2, 2020 · 41 comments
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version needs spec

Comments

@imankulov
Copy link

Environment data

  • Language Server version: 2020.6.1
  • OS and version: macOS Mojave 10.14.6
  • Python version: 3.8.2, installed via pyenv.

Expected behaviour

As demonstrated in the in Pylance announcement, I want to be able to automatically import packages as I type.

  • I type "gc" and make a short pause
  • VSCode shows the tooltip, inviting me to import gc
  • I choose the library from the popup window
  • VSCode adds an import to the top of the file

Actual behaviour

  • I type "gc" and make a pause
  • Nothing happens

Logs

Logs output
[Info  - 11:15:42 AM] Pylance language server 2020.6.1 starting
[Info  - 11:15:42 AM] Server root directory: /Users/roman/.vscode/extensions/ms-python.vscode-pylance-2020.6.1/server
[Info  - 11:15:42 AM] No configuration file found.
[Info  - 11:15:42 AM] Setting pythonPath for service "vscode-playground": "/Users/roman/workspace/vscode-playground/env/bin/python3"
[Info  - 11:15:42 AM] stubPath /Users/roman/workspace/vscode-playground/typings is not a valid directory.
[Info  - 11:15:42 AM] Assuming Python version 3.8
[Info  - 11:15:42 AM] Assuming Python platform Darwin
[Info  - 11:15:42 AM] Searching for source files
[Info  - 11:15:42 AM] Auto-excluding /Users/roman/workspace/vscode-playground/env
[Info  - 11:15:42 AM] Found 1 source file
[FG] parsing: /Users/roman/workspace/vscode-playground/foo.py (7ms)
[FG] parsing: /Users/roman/.vscode/extensions/ms-python.vscode-pylance-2020.6.1/server/typeshed-fallback/stdlib/2and3/builtins.pyi (245ms)
[FG] binding: /Users/roman/.vscode/extensions/ms-python.vscode-pylance-2020.6.1/server/typeshed-fallback/stdlib/2and3/builtins.pyi (95ms)
[FG] binding: /Users/roman/workspace/vscode-playground/foo.py (0ms)
[Info  - 11:15:42 AM] Background analysis root directory: /Users/roman/.vscode/extensions/ms-python.vscode-pylance-2020.6.1/server
[Info  - 11:15:42 AM] Background analysis started
[BG] analyzing: /Users/roman/workspace/vscode-playground/foo.py ...
[BG]   parsing: /Users/roman/workspace/vscode-playground/foo.py (41ms)
[BG]   parsing: /Users/roman/.vscode/extensions/ms-python.vscode-pylance-2020.6.1/server/typeshed-fallback/stdlib/2and3/builtins.pyi (246ms)
[BG]   binding: /Users/roman/.vscode/extensions/ms-python.vscode-pylance-2020.6.1/server/typeshed-fallback/stdlib/2and3/builtins.pyi (106ms)
[BG]   binding: /Users/roman/workspace/vscode-playground/foo.py (0ms)
[BG]   checking: /Users/roman/workspace/vscode-playground/foo.py (2ms)
[BG] analyzing: /Users/roman/workspace/vscode-playground/foo.py (396ms)
[FG] parsing: /Users/roman/workspace/vscode-playground/foo.py (3ms)
[FG] binding: /Users/roman/workspace/vscode-playground/foo.py (3ms)
[BG] analyzing: /Users/roman/workspace/vscode-playground/foo.py ...
[BG]   parsing: /Users/roman/workspace/vscode-playground/foo.py (2ms)
[BG]   binding: /Users/roman/workspace/vscode-playground/foo.py (0ms)
[BG]   checking: /Users/roman/workspace/vscode-playground/foo.py (0ms)
[BG] analyzing: /Users/roman/workspace/vscode-playground/foo.py (3ms)
[FG] parsing: /Users/roman/workspace/vscode-playground/foo.py (1ms)
[FG] binding: /Users/roman/workspace/vscode-playground/foo.py (0ms)
[BG] analyzing: /Users/roman/workspace/vscode-playground/foo.py ...
[BG]   parsing: /Users/roman/workspace/vscode-playground/foo.py (1ms)
[BG]   binding: /Users/roman/workspace/vscode-playground/foo.py (0ms)
[BG]   checking: /Users/roman/workspace/vscode-playground/foo.py (2ms)
[BG] analyzing: /Users/roman/workspace/vscode-playground/foo.py (3ms)

Code Snippet / Additional information

vscode

@gramster
Copy link
Member

gramster commented Jul 2, 2020

Try this:

  • Type gc.
  • You should get a squiggle under gc
  • If you hover on gc you should get a floating error popup saying it is not defined
  • If you click on gc you should get a 'quick fix' lightbulb. and if you click on that you should get the option to add the import

@huguesv
Copy link
Contributor

huguesv commented Jul 2, 2020

Also note that if you're trying to activate it with the keyboard, you'll only get a suggestion if the caret is within the identifier (in this case, after g and before c). If the caret is before or after the identifier, the lightbulb will not appear.

@Destaq
Copy link

Destaq commented Jul 2, 2020

Try this:

  • Type gc.
  • You should get a squiggle under gc
  • If you hover on gc you should get a floating error popup saying it is not defined
  • If you click on gc you should get a 'quick fix' lightbulb. and if you click on that you should get the option to add the import

@gramster This works, but I thought that it is auto-imported? Having to hover, click, and click every time sort of takes away the purpose and automation of auto-imports.

@huguesv
Copy link
Contributor

huguesv commented Jul 2, 2020

Oh, sorry, I also confused light bulb with auto import. It's not auto importing for gc as shown in the animated gif from the blog post.

@heejaechang can you help?

@heejaechang
Copy link
Contributor

unlike code action, since auto import shows up in completion list, we try not to include all possible identifiers in the completion. we only show ones already have used. we could tweak this behavior. but that is our current behavior.

that being said, we can improve code action to show it at the identifier edges.

@Destaq
Copy link

Destaq commented Jul 2, 2020

So is there support for the libraries being automatically imported when you type in e.g. gc.disable()? Because that doesn't seem to be happening, contrary to the introduction video.

@heejaechang
Copy link
Contributor

it depends, for example, if you have 2 files open, and one of the files have gc used, then completion will have "gc" in the other file. if you have "import statement" in a current file that implicitly imports "gc", then "gc" will show up in completion.

otherwise, completion could have too many entries from all library files accessible from the current file. currently, code action can be used for this situation, and then related libraries will start to show up in auto import.

but that being said, we can improve auto-import behavior based on feedbacks.

@imankulov
Copy link
Author

Thanks for clarifying, @heejaechang. Now it makes more sense, and I understand that it works as intended.

With this saying, I should admit that the auto-import behavior is quite confusing because it works in a limited set of scenarios. For example, to import gc, you need to have a different file where you imported the module before. The file should be open in your session, and you should interact with this file in a special way (the editor should auto-complete the contents of gc. for you at least once.) Then you can close the file, but if you close the session and re-start it again, you lose the auto-import for the module again.

I recorded a second video, and it's available here: https://ibb.co/B6MJDBM (the file was too large to upload on GitHub.)

Since it's not a bug, I don't mind if you close this issue.

@Destaq
Copy link

Destaq commented Jul 3, 2020

Yes, maybe I'm missing some resource, but I don't see any detailed information about PyLance other than the introductory README.md and blog post. If you haven't set it up, I believe that it would be useful for potential users to have a page/file they can go to in order to see all of the features of PyLance.

For example, the auto-import information should be included there, as well as docstrings, detail on each of the features, etc.; many features are listed but only a few are demonstrated with video.

@erictraut
Copy link
Contributor

Thanks for the input. I agree that the auto-import behavior is confusing, and there are opportunities to improve it further.

It tends to work better if you set the diagnostic mode to tell pylance to analyze all of the files in your workspace rather than just the open files.

"python.analysis.diagnosticMode": "workspace"

I also like the idea of providing additional documentation for Pylance. In the meantime, the Pyright site may provide you with some of the information you're looking for. Pylance is built on Pyright, so most of the functionality is the same.

@imankulov
Copy link
Author

@erictraut, thanks for the hint. Meanwhile, setting "python.analysis.diagnosticMode": "workspace" hasn't changed the behavior of the auto-imported. I can see from the console that the extension analyzed closed files as well, but it didn't make the auto-complete work.

@imankulov
Copy link
Author

I allow myself to close down the issue. It's not a bug, and I think there's no immediate actionable to change the behavior. Thanks a lot for explaining how the feature works.

@JosephTLyons
Copy link

JosephTLyons commented Jul 5, 2020

Even after hearing the explanation, I'm still confused as to why I can't get automatic imports to work the same as the gif shows. It seems a bit misleading.

Even with other files open that have the same import, I am not getting auto-import suggestions in another file in the same project.

With that being said, I'm really digging Pylance a lot. Thanks for all of the hard work.

@erictraut
Copy link
Contributor

I'm going to reopen this because I think it's worth investigating further. At the very least, we need to document the current behavior better. And we should look for ways to improve the behavior.

@erictraut erictraut reopened this Jul 5, 2020
@JosephTLyons
Copy link

JosephTLyons commented Jul 5, 2020

From what I read, the reason why an import doesn't just magically show up as auto-import suggestion (unless another file has recently imported it) is because there would be too many suggestions in the autocomplete/auto-import menu if it had to show them for every character types... I think a good middleground would be to show code completions as you type (like normal), then only show possible auto-imports after the . is typed on a bit of code.

So if you had a variable called "date" and you typed in da somewhere, the autocomplete list would show date as a possible autocomplete option... but if you typed datetime., then the import for datetime would show up. It would not populate the autocomplete list until the . was inserted. This would keep the amount of items in the autocomplete list relatively low in most cases.

Just a suggestion.

@taldcroft
Copy link

Somewhat related, I finally understood why this was not working for me at all. I was doing something like this:

template = jinja2.

It seems that the auto-import functionality is only triggered when the package name is at the beginning of the statement, but of course this is most commonly not the case. Am I getting that right and can this behavior be improved?

@VolkerH
Copy link

VolkerH commented Jul 6, 2020

yes, please document this prominently as it is prominently featured in the blog post. I saw the blog, thought "great", tried with a numpy import (didn't work), tried to replicate the behaviour from the blog post with .gc., uninstalled/reinstalled pylance, tried again until I decided to look in the issues.

@ebsaral
Copy link

ebsaral commented Jul 9, 2020

Screen Recording 2020-07-09 at 05 29 36 PM

I am working in a Django project.

As seen in this GIF, auto-import should import as from django.core.paginator import Paginator but case insensitivity causes a problem and it gets from django.core import paginator instead. I couldn't make this work right. Leaving it as an example. I also couldn't make auto-import work inside a function.

@BrennanAyers
Copy link

Hi all, coming to VSCode from PyCharm to be able to use LiveShare in these full remote times. Glad to see I'm not the only one confused by this implementation/lack of functionality around auto imports.
Just wondering why the solution isn't something along the lines of what JetBrains has already done with their IDEs: https://www.jetbrains.com/help/pycharm/creating-and-optimizing-imports.html
With the specific hotkey you don't flood Intellisense with extraneous information, and you also don't have to search for the import every time you type something, only when triggered. Coming into a new codebase, this feature is a lifesaver and it would be great to see it in Pylance.
Thanks!

@judej judej added enhancement New feature or request needs spec labels Jul 9, 2020
@heejaechang
Copy link
Contributor

we will work on improving this feature based on feedbacks in future releases! we are already looking into it! hopefully, we have something to show soon!

@oesah
Copy link

oesah commented Jul 14, 2020

To sum my opinion up: Great improvement overall, but this auto-import is still not a good solution. I am wondering, why does it Work so well with Type-/Java-Script? Javascript usually has much more libraries, but it still works super fast and accurate.

@heejaechang
Copy link
Contributor

we just didn't implement some of the things that require more CPU and memory to work. eventually, hopefully we can add functionalities that people want based on feedback!

@bkcsfi
Copy link

bkcsfi commented Jul 31, 2020

Is there a way to disable auto-import suggestions/function?

I cannot find a setting to control it.

I'm finding my modules are randomly failing to load because weird imports have been pasted in at the top, these are invalid imports (i.e. importing something that doesn't exist)

Sometimes the UI is janky as I type, I suspect the UI sometimes thinks I'm trying to pick an auto-import. I would like to turn off this feature.

For myself, I don't like auto-imports because I want to be explicit about what gets imported. I can see others might find it handy.

@jakebailey
Copy link
Member

@bkcsfi No, but you can upvote #64.

@bkcsfi
Copy link

bkcsfi commented Jul 31, 2020

Ooops, sorry I missed that. Upvoted.

@heejaechang heejaechang added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Sep 2, 2020
@heejaechang
Copy link
Contributor

heejaechang commented Sep 2, 2020

should be fixed in the next release.

autoimportfix

@jakebailey jakebailey reopened this Sep 2, 2020
@jakebailey
Copy link
Member

This issue has been fixed in version 2020.9.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#202090-3-september-2020

@hmoravec
Copy link

I have still issues with auto-imports, e.g. auto-import of Path from pathlib doesn't work:
image

Only when I manually added from pathlib import Path and then deleted it again, the auto-import suggested to Add import Path from pathlib:
image

@jakebailey
Copy link
Member

The code that fixed this issue was disabled for performance reasons while we could optimize it. Can you set "python.analysis.indexing": true and see if your issue is resolved and don't have too much extra performance degredation?

@hmoravec
Copy link

Seems to be working now, even though VS Code shows warning "Unknown Configuration Setting" for this option in settings.json.

@jakebailey
Copy link
Member

Yes, it's a hidden toggle for testing. We're hoping to get that enabled for everyone soon, so thanks for confirming that it's working out.

@hmoravec
Copy link

The auto-import still is not working in some cases. I have defined variable (type alias) ImagePil = PIL.Image.Image in one module. When I type ImagePil as a type annotation in a function in other module, VS Code does not offer any actions (even when I added the import manually at first and then deleted it). When I try to invoke Quick fix command, it only shows No code action available and the language server logs Background analysis message: getDiagnosticsForRange and nothing happens.

@erictraut
Copy link
Contributor

Type aliases are not currently included in the index, but this support was just added. The challenge with type aliases is that they are syntactically identical to variable assignments in Python, so without a full type analysis, it's not possible to tell them apart in a definitive manner. And full type analysis is way too expensive to do for every library file during indexing. The solution we've converged upon is to use simple naming heuristics to do a "good enough" job of distinguishing variables from type aliases, since the latter are typically named using capitalized camel-casing. You'll see this change in the next release of pylance.

@hmoravec
Copy link

hmoravec commented Nov 17, 2020

I bumped into another problem: I have package pdf2image in my virtual environment in Docker container. I can see in the language server logs:

[BG(1)]     parsing: /opt/venv/lib/python3.8/site-packages/pdf2image/pdf2image.py [fs read 0ms] (33ms)
[BG(1)]     binding: /opt/venv/lib/python3.8/site-packages/pdf2image/pdf2image.py (4ms)
[BG(1)]     parsing: /opt/venv/lib/python3.8/site-packages/pdf2image/generators.py [fs read 0ms] (1ms)
[BG(1)]     binding: /opt/venv/lib/python3.8/site-packages/pdf2image/generators.py (0ms)

But when I type pdf2image, VS Code does not offer to import this package.

Further, if I type Image, it does not offer to import Image from PIL package, only from tkinter and six other unrelated imports.

@AndrewTrapani
Copy link

The auto import functionality and suggestions wasn't working as I expected today with:
VS Code 1.51.1
PyLance 2020.12.0
Python 2020.11.37...

Fixed some issues I was having today by adding this hidden setting to "settings.json"
"python.analysis.indexing": true
Press ctrl+shift+p to bring up the "Command Palette" (View menu -> Command Palette) and I found "Preferences: Open Settings (JSON)" by starting to type "settings.json"
NOTE: There was also workspace settings, if you wanted to set it for just one workspace.

This hidden "feature" fixed an issue where when I typed os. it wouldn't suggest an import in any of the ways I'd have expected:
Type os. and a menu doesn't pop up with an Auto Import option.
Type os. and mouse over os, and Quick Fix ... doesn't appear.
Type os and no light bulb appears. Mouse over and still no light bulb.

When I enabled the above setting
Type os and a menu pops up with an Auto Import option.
Type os or os. and mouse over os, and Quick Fix ... appears and works. Clicking on it opens a menu to suggest one of many options (if any) as "Add import os".

Similarly, works with gc and guessing if the user meant something else:
Type gc and press "Ctrl" + "." to bring up a menu "Add import gc" and "Add import gcd from math"

NOTE: gc was not imported AFAIK into any other files in the workspace.

@anthonyalayo
Copy link

anthonyalayo commented Jan 24, 2021

The code that fixed this issue was disabled for performance reasons while we could optimize it. Can you set "python.analysis.indexing": true and see if your issue is resolved and don't have too much extra performance degredation?

Hey there, new to the thread. I just got up to speed with Pylance in VS Code and without this setting on, I was unable to get any autocompletion for imports. Now it is working.

@hipertracker
Copy link

I can't find python.analysis.indexing. There is no such option in the latest Pylance.

@jakebailey
Copy link
Member

It is a hidden option, and must be set directly in your settings.JSON.

@liudonghua123
Copy link

It is a hidden option, and must be set directly in your settings.JSON.

Thanks!

@tonisives
Copy link

tonisives commented Dec 16, 2021

Would it make sense to enable python.analysis.indexing by default?

I write pytest and it recommends ByteString without the setting.

Screenshot 2021-12-16 at 16 18 38

With setting enabled:
Screenshot 2021-12-16 at 16 19 44

@davidonlaptop
Copy link

YMMV but it seems that the default indexing isn't deep enough for auto-import to work when the function or class you import is not at the root level, so here's what I had to put in my settings.json for it to work properly:

{
  "python.analysis.autoImportCompletions": true,
  "python.analysis.indexing": true,
  "python.analysis.packageIndexDepths": [
    {
      "name": "",
      "depth": 3
    },
    
    ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version needs spec
Projects
None yet
Development

No branches or pull requests