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

Import resolution errors should show more information about the environment in use #4368

Open
Tracked by #3892
rchiodo opened this issue May 15, 2023 · 8 comments
Open
Tracked by #3892
Assignees
Labels
enhancement New feature or request

Comments

@rchiodo
Copy link
Contributor

rchiodo commented May 15, 2023

We had a hypothesis that users were not picking virtual environments inside of VS code.

This was validated during a recent QP study.

One potential solution is to:

  • Tell the user which virtual environment can resolve the paths
  • Give a quick action to switch to that path
@rchiodo
Copy link
Contributor Author

rchiodo commented May 30, 2023

@karthiknadig, was wondering if you had any thoughts on this.

The idea is to change import errors to:

  • Search the user's list of environments (we'd use the environment api from python extension) to find environments that have missing imports
  • If there's an environment that resolves one or more of the import errors, suggest that the user switches to that environment.
  • Provide a quick action to actually do the switch (which would use the python extension's api to set the new environment).

@karthiknadig
Copy link
Member

I like the idea, but from implementations perspective there are few things to consider since it can significantly impact performance.

Search the user's list of environments (we'd use the environment api from python extension) to find environments that have missing imports

This might get very expensive. What we have seen is that people generally create environments globally and don't really clean up. That means that often people have hundreds of environments. Searching each of these for list of installed packages can be very expensive. So, we need to be creative about how we look for things. /cc @karrtikr : we might need to suggest selecting a environment more strongly if there is one already available in user's workspace.

If there's an environment that resolves one or more of the import errors, suggest that the user switches to that environment.

This would have to be heuristic. The heuristic would be how closely matched is the environment to the one expected by the user. Since version of packages or python also has impact on if the user is able to import something or not.

@gramster
Copy link
Member

gramster commented Jun 6, 2023

If we can at least know what we are looking for for some set of imports, we may not have to actually search for something that satisfies an import, but can just see if the expected package is present. I'd suggest doing that based on timestamps too, so we check recently created environments first. We can also check venv files to try short-circuit this.

@rchiodo
Copy link
Contributor Author

rchiodo commented Jun 6, 2023

There were more ideas, figured I'd save them here:

Other ideas include:

  • Switching their current environment to match the one in the terminal automatically
  • Notifying them when they open the terminal and we notice that it's different (instead of just automatically switching it)
  • Auto creating a new virtual environment when first opening a folder that matches their requirements.txt (although not sure about this one)

@rchiodo
Copy link
Contributor Author

rchiodo commented Jun 7, 2023

Minispec

  • Add extra message to import resolve errors
  • Current environment is: X
  • Extra message gives a hint to switch or create a new environment that includes that module
  • Add telemetry to indicate how many people click on it

@cwebster-99
Copy link
Member

Spec based on the discussion in microsoft/vscode-python#21456

Message:

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

Quick Actions:

  • Select a different interpreter
  • Learn more ....

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 and/or https://code.visualstudio.com/docs/python/editing#_troubleshooting-intellisense

I can work on drafting the information in the documentation for "Learn more"!

@rchiodo rchiodo changed the title Import resolution errors could search the user's virtual environments Import resolution errors should show more information about the environment in use Jul 21, 2023
@PylanceBot PylanceBot added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Jul 24, 2023
@rchiodo
Copy link
Contributor Author

rchiodo commented Jul 26, 2023

This issue has been fixed in prerelease version 2023.7.41, which we've just released. You can find the changelog here: CHANGELOG.md

@rchiodo rchiodo closed this as completed Jul 26, 2023
@rchiodo
Copy link
Contributor Author

rchiodo commented Dec 5, 2023

We actually backed out half of this. I forgot. We need to reimplement this behind an experiment.

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

No branches or pull requests

7 participants