others-How to solve ModuleNotFoundError: No module named 'CommandNotFound' error when running a python script?
1. Purpose
In this post, I would demo how to solve the following error when running a python script:
root@launch-advisor-20191120:~# rma
Traceback (most recent call last):
File "/usr/lib/command-not-found", line 27, in <module>
from CommandNotFound.util import crash_guard
ModuleNotFoundError: No module named 'CommandNotFound'
2. The problem and solution
2.1 What is the python version and environment variables in my system
Ubuntu 18.04.6 LTS
Ubuntu SMP Wed Dec 9 06:57:35 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
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:
root@launch-advisor-20191120:~# rma
I got this:
Traceback (most recent call last):
File "/usr/lib/command-not-found", line 27, in <module>
from CommandNotFound.util import crash_guard
ModuleNotFoundError: No module named 'CommandNotFound'
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:
export PYTHONPATH=$PYTHONPATH:/usr/local/python3/lib/python3.8/site-packages:/usr/lib/python3/dist-packages
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:
root@launch-advisor-20191120:~# env|grep PYTHONPATH
PYTHONPATH=:/usr/local/python3/lib/python3.8/site-packages:/usr/lib/python3/dist-packages
root@launch-advisor-20191120:~#
2.3.2 Solution #2
Or we can just remove the not found
module:
apt-get remove command-not-found
2.3.3 Solution #3
This error can be fixed by changing the first line of /usr/lib/command-not-found
, from:
#!/usr/bin/python3
to:
#!/usr/bin/python3.8
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:
sudo rm -rf /usr/bin/python3
Then recreate the soft link to python 3.8 installation:
sudo ln -s /usr/bin/python3.8 /usr/bin/python3
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.