The test about the suggestion of "ModuleNotFoundError"

See here. Now the suggestion of the name is stable. Here is the test of “test.py” in the storage(what add is: ("import abd", ModuleNotFoundError, "no module named 'abd'. Did you mean 'abc'?")):

No. number of entries used time(/s) average time(/s) result
1 5 0.093 0.019 success
2 5 0.087 0.017 success
3 5 0.079 0.016 success
4 5 0.078 0.016 success
5 5 0.089 0.018 success
6 6 0.086 0.014 success
7 6 0.080 0.013 success
8 6 0.086 0.014 success
9 6 0.089 0.015 success
10 6 0.076 0.013 success

See the column, we found that the number of entries were almost no effect. So the most of time cost by IDLE and the start of unittest, and the time that found the most likely module is nearly null.

Testing the speed of the find_all_packages.scan_dir("path/to/site-packages"), I got the data below:

Tools number average time(/ms)
timeit 1000 5.795

The len of the site-packages on my computer is 236. So that I can assert that before the number of the packages effect the function to scan the site-packages, the devices have tried to forbidden to install anything due to the capacity of the storage. The cache is useless.

More about why cache is useless

The reason of “building a cache” assume that the suggestion of the “ModuleNotFoundError” will effect the normal import. However, before the error diffuse, the function that suggest the name in traceback has not run, so the normal import is not effected. And consider about the normal code structure:

#1. import module
import module1
import module2
from module3 import name1
from module4.child_module import name2

#2. define the variable
a = 1
def b():
    ...
class C:
    def __init__(self):
        ...
    ...

def main():
    ...

#3. running the script
if __name__ == "__main__":
    main()

The ModuleNotFoundError often appears at the stage of import module, so there is no confuse that “How it cost when suggest the name”, the main module has stopped. The only space is the delay import, but as I has said, if you caught the exception, the suggest wouldn’t run. Hadn’t catch, the effect was also small and the script has been needed to stop.

The other

If the user is forget to install the packages, is the suggest helpless?

Well, this is the question for all of the auto suggestion: there must be some suggestions that is wrong. For example, I made a mistake in spelling in the define but spelt it right when use, the NameError won’t suggest that change the name in defination, can only suggest you to use the wrong name.
So, is it possible to suggest the users to “pip install xxx”? No! That is DANGEROUS. This is helping the hackers to damage users due to that wrong spelling is often with Homograph Attack.

Thanks

@aroberge

1 Like

I think you suggested this before. Did things change meanwhile?

For example, the import of module.child_module didn’t change in 3.14. However, I changed the format to “module ‘module’ has no child module ‘child_module’” to better to show the level.

And now I had made all of what suggested in that suggestion. There is almost no change.

If you want to improve the suggestions in the REPL, you can open pull requests against Python and we’ll review them.

If I said that the change now is not only REPL, what’s your reaction?
However, now I only test it on windows, and the way to package it is “nanobind”. So what to do to combine it with cpython? If somebody suggests, I can’t express thanks enough.

Now that there are many ideas from different developer, and some are comflict. Now I want a exactly one final idea.
Now the test in 3.15 on my computer here (seven entries):