在ubuntu16.04中输入不存在的shell命令时,报错ModuleNotFoundError的(靠谱)解决方案

 

在ubunutu 16.04上使用源码编译安装python3.8之后出现了这个bug。任意输入不存在的命令,会跳出来一个python的异常

报错详情:

root@Lee:/var/log# junk
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'

搜索标题中的文字,会得到cnblogs上的一篇文章:

https://www.cnblogs.com/finlu/p/11588870.html

这篇答案会指向 https://askubuntu.com/questions/766246/missing-package-commandnotfound

解决方案是。。删除掉python3。。可是我不想删除刚刚装好的python行不行。

sudo apt-get remove --purge python3*
sudo apt auto remove

继续找了好久,发现有各种各样的解决方案(下面两个是不太靠谱的):

  • 不要使用源码安装python
  •  将usr/bin里面的python3符号链接恢复到指向系统自带的python3.5

最后找到一个靠谱的:https://askubuntu.com/questions/952302/python-modulenotfounderror-commandnotfound

Add one more answer, as I also encountered this issue and happened to solve it.
This issue happened when I installed python3.7 from source.

Check /usr/lib/command-not-found file, it is trying to find module CommandNotFound, which is NOT shipped with python3.7 but DOES with Ubuntu under directory /usr/lib/python3/dist-packages.

What we need to do is adding the directory to python import path:

  1. vi .bashrc (or .profile)
  2. add line
    export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.7/site-packages:/usr/lib/python3/dist-packages
    Pay attention that you may need to change the /usr/local/lib/python3.7/site-packages to your own directory.
  3. exit and source .bashrc (or .profile)
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值