-
Notifications
You must be signed in to change notification settings - Fork 767
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
Comments
Try this:
|
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 |
@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. |
Oh, sorry, I also confused light bulb with auto import. It's not auto importing for @heejaechang can you help? |
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. |
So is there support for the libraries being automatically imported when you type in e.g. |
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. |
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 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. |
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. |
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.
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. |
@erictraut, thanks for the hint. Meanwhile, setting |
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. |
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. |
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. |
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 So if you had a variable called "date" and you typed in Just a suggestion. |
Somewhat related, I finally understood why this was not working for me at all. I was doing something like this:
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? |
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 |
I am working in a Django project. As seen in this GIF, auto-import should import as |
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. |
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! |
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. |
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! |
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. |
Ooops, sorry I missed that. Upvoted. |
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 |
The code that fixed this issue was disabled for performance reasons while we could optimize it. Can you set |
Seems to be working now, even though VS Code shows warning "Unknown Configuration Setting" for this option in |
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. |
The auto-import still is not working in some cases. I have defined variable (type alias) |
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. |
I bumped into another problem: I have package
But when I type Further, if I type |
The auto import functionality and suggestions wasn't working as I expected today with: Fixed some issues I was having today by adding this hidden setting to "settings.json" This hidden "feature" fixed an issue where when I typed When I enabled the above setting Similarly, works with NOTE: |
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. |
I can't find |
It is a hidden option, and must be set directly in your settings.JSON. |
Thanks! |
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
},
]
} |
Environment data
Expected behaviour
As demonstrated in the in Pylance announcement, I want to be able to automatically import packages as I type.
Actual behaviour
Logs
Logs output
Code Snippet / Additional information
The text was updated successfully, but these errors were encountered: