cannot import class, even though quick fix founds it

Viewed 430

In my code I use the not yet imported class NoSuchElementException. If I select the class and do "import class" or "expand class", I get "The class is not found.".

But VS code proposes a fix (yellow bulb), and if I click on it, it imports the class at the top :

from selenium.common.exceptions import NoSuchElementException

If VS code can find the class, why "import class" does not find it ?

Even with a simple class name like "By", vscode quickly finds the possible classes, and I do "quick fix" :

vscode quick fix

But I'd like to do something like "import class" or "expand class" myself, instead of using "quick fix".

2 Answers

VS Code has scanned all of the modules it can find and has built up a database of possible solutions. It takes a guess on what you might want and shows it to you. This is rather computation intensive, which is fine when you are editing in an IDE, but burdensome for every load of a python program. And the guess is just that: a guess. You can't fully automate something that is so prone to error. Its reasonable to think that multiple packages would implement something called "NoSuchElementException". What about something really common such as "Path" or "Application"?

Python has a set of rules about namespaces and where modules are stored on disk to avoid ambiguities at runtime.

I think it has something to do with the multiple folders in the python directory installed on your computer. The folder which you try to import is deep in the python directory. If you use Import NoSuchElementException the program can't find it because it doesn't goes as deep as it needs to be to find the folder.

That's why you need to specify selenium.common.exceptions. So the program knows in which folders to search for NoSuchElementException

I hope this answer helps you.

Related