I noticed the same! And I've found that there are several, similar but subtly different causes for this problems. In the most basic case, a user just forgot to install a package using pip install <package>
. However, in many other cases the user did execute command, but in another Python environment, so that they still got the ModuleNotFoundError
when executing their code. Sometimes it depends on how they execute the code.
I've written quite a comprehensive answer for one of these questions, trying to explain everything needed to know to understand why this happens and how to solve it. Since then I try to direct persons with the same question to that answer in the hope that this answer would solve this problem "once and for all" ;-) Otherwise the same "interview" (did you try this? did you try that? how did you install it? how did you run it? etc.) has to be repeated everytime...
My answer focuses on Windows, but some of it is true for other operating systems as well. I still want to extend my answer with some info about virtual environments, but I think it is a useful answer already. Suggestions/improvements are welcome of course. Please leave a comment and I'll try to improve it if necessary.
What do you think? Could this be a canonical answer for (many, probably not all) ModuleNotFoundError
-questions?
ModuleNotFoundError
. I'm proposing that we make one canonical forModuleNotFoundError
s caused by a module not being installed or being installed correctly. A canonical answer for this wouldn't really be that long.