How to resolve ModuleNotFoundError: No module named 'CommandNotFound' error when running a python script?
1. Purpose
In this post, I will demonstrate how to resolve the following error when running a Python script:
2. The problem and solution
2.1 What is the python version and environment variables in my system
2.2 What is in the File /usr/lib/command-not-found?
rma is a python script to analyze redis dump files, it depends on python3, but on my system, there is already a python2 installed, After installing python3, I tried to run rma like this:
I got this:
According to Faheem Mitha:
command-not-found
is a python program, which runs when your command is not something found on the system. (Its function is to suggest alternatives and corrections in case of mistyping etc.) See/usr/bin/command-not-found
. It is trying to import theCommandNotFound
module and is unable to, clearly pointing to a screwed up python installation
2.3 How to solve the problem?
2.3.2 Solution #1
It seems that python can not find packages, so we need to change the env:
PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. For most installations, you should not set these variables since they are not needed for Python to run. Python knows where to find its standard library.
Check and verify the PYTHONPATH environemental variable:
2.3.2 Solution #2
Or we can just remove the not found
module:
2.3.3 Solution #3
This error can be fixed by changing the first line of /usr/lib/command-not-found
, from:
to:
Make sure the python verison is aligned with your system.
The first line in a python script is the “shebang” line. When you execute a script file from the shell, the shell tries to run the file using the command specified on the shebang line. The ! is called the “bang”. The # is not called the “she”, so sometimes the “shebang” line is also called the “hashbang”.
Why do we need the first line of python script?
By specifying #!/usr/bin/python you specify exactly which interpreter will be used to run the script on a particular system. This is the hardcoded path to the python interpreter for that particular system. The advantage of this line is that you can use a specific python version to run your code.
2.3.4 Solution #4
Remove the soft link to python 3:
Then recreate the soft link to python 3.8 installation:
2.4 Why did this problem occur?
According to :
After I opened the
/usr/lib/command-not-found
, I realized this error is due to system executing this script using the newly installed python3.5.0, because installing Python3.5.0 creates leads the system to use it when you type inpython3
.
This error is caused by the screwed up python installations. For me, the problem occurred after installing the python3 on my ubuntu system.
3. Summary
In this post, I tried to demonstrate how to fix the ModuleNotFoundError: No module named 'CommandNotFound'
when running any python script , the key point is to organize your python installations , do not mess it up. That’s it, thanks for your reading.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
- 👨💻 User Faheem Mitha
- 👨💻 Ubuntu – Error
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!