microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.32k stars 1.18k forks source link

Improve messaging when an import could not be resolved #21456

Closed cwebster-99 closed 10 months ago

cwebster-99 commented 1 year ago

Background

A common hurdle new users encounter when using Python in VS Code is package installation. Those new to Python programming and working with editors find it challenging to understand the concepts of installing and importing packages, using pip, and managing environments. This complexity can lead to frustration and some users may even abandon Python in VS Code, perceiving it as unfriendly and intimidating for beginners.

Audience

The target audience for the proposed mockup is beginners who may not be comfortable or familiar with the idea of environments or third-party packages.

Current Behavior

Unresolved import messaging from Pylance

image

Current Code Actions

image

Proposed Behavior

To address some of the confusion around environments and package installation, we should provide specific Code Actions and/or specific messaging tailored for users that are not using an environment with their specified packages or struggle to resolve import errors.

💡“Select another interpreter”

💡 “Learn how to install packages” (https://github.com/microsoft/pylance-release/discussions/3297)

Option 1: Present options in the Quick Fix list

image

Option 2: Provide a link at the bottom of the Quick Fix list and provide more info on hover

image

image

Example from TS:

image

cc @luabud

rchiodo commented 1 year ago

I think we should also add a better message to the message from Pylance. We have an issue for that here: https://github.com/microsoft/pylance-release/issues/4368

cwebster-99 commented 1 year ago

@rchiodo Are you referencing this message (the tip added for the purpose of the spec) image

Would the messaging you have in mind be to list the current environment the user is working in and then suggest switching or creating a new environment?

rchiodo commented 1 year ago

I was thinking it would say something like so:

Import "flask" could not be found in the ".venv" environment. (reportMissingImports).

Then the tip you have would show up. And the quick actions would have the options you mentioned above.

rchiodo commented 1 year ago

I think the hard part here is adding that 'Tip' item. It would be relatively easy for us to add it on the Pylance side, but having the Python extension do so would be a lot harder.

rchiodo commented 1 year ago

I also think the two options you have would be good combined as well. So the entire change would be:

Message:

Import "flask" could not be found in the ".venv" environment. (reportMissingImports). Tip: Ensure you are using the correct interpreter

Quick actions:

With maybe the middle two actions not in the MVP

cwebster-99 commented 1 year ago

I really like this idea for the MVP!

My only concern about the "Select another interpreter" as a Code Action is that it does not directly add code. Maybe it would be sufficient to just have the tip in the message? @luabud Do you have an opinion on this?

rchiodo commented 1 year ago

I'm also thinking it makes the most sense for Pylance to make all of these changes. Otherwise, we're going to have to add something to diagnostics that allows the Python extension to figure out that it's an import error, and what package is missing.

luabud commented 1 year ago

Love the idea! Agreed with making this on the Pylance side. I wonder if it'd be possible to make the "select interpreter" command be triggered from a link in the tip? then it wouldn't need to be a code action?

rchiodo commented 1 year ago

I believe that's possible, but I'm wondering why not use a code action?

Pylance can initiate a command from a code action, so it's certainly possible to 'Select interpreter' in a code action from Pylance.

luabud commented 1 year ago

I guess I was coming from the point of view that it's not really a code action as code isn't being touched/changed but I chatting with Brett he did bring up that it might make code no longer fail to execute, so a code action still makes sense. No objections against code actions anymore from me :p

cwebster-99 commented 1 year ago

Awesome! I think the MVP should be as Rich outlined above contributed by Pylance:

Message:

Import "flask" could not be found in the ".venv" environment. (reportMissingImports). Tip: Ensure you are using the correct interpreter

Quick Actions:

For the "Learn more", I think it would be best to add a section to https://code.visualstudio.com/docs/python/environments#_working-with-python-interpreters or https://code.visualstudio.com/docs/python/editing#_troubleshooting-intellisense with information on how to troubleshoot the error message. Preferences on one or the other?

rchiodo commented 1 year ago

Both :)

I mean if somebody is at either location, they should be able to learn the same information. Maybe both can point to another URL that describes the situation (or one points to the other).

cwebster-99 commented 10 months ago

Closing this in favor of the https://github.com/microsoft/pylance-release/issues/4368 that tracked this work from the Pylance side.